Damp — File Browser

Dampv1.3.0 / Dampv1.3.0 / damp.mrc

damp.mrc — 70.81 KB — Download this file

Damp
;mIRC version check
on *:load: {
  if ($version < 6.03) { 
    decho
    decho You have mIRC version $version
    decho Damp is not gurantee to work!
    decho -
  }
  decho Usage:
  decho /damp [-s]
  decho If you specify the -s parameter, you will be asked to select a skin instead of using the previous skin.
}
on *:unload: {  cmd.button.exit }
alias damp.version {
  goto $1
  :1 | return 1.3.0
  :2 | return 20030329
  :3 | return 030329
}
alias dampdir return $scriptdir
menu menubar,status,channel,query {
  &Damp $damp.version(1)  $+ ( $+ $damp.version(3) $+ )
  .&Run: damp
  .-
  . $iif(%damp.dockwin,$style(1)) &Dock playlist: set %damp.dockwin $iif(%damp.dockwin,0,1) | if (%damp.dockwin) { damp.dockwin }
  .$iif(%damp.autosave,$style(1)) Auto save playlist: %damp.autosave = $iif(%damp.autosave,0,1) | return $damp.autosave
  .-
  .Search playlist: damp.search
  .-
  .&Skin
  .. $submenu($listskin($1))
  .&Browse skins: listallskin
  .Set skins &directory: %damp.skindir = $$sdir($skindir,Select a directory,Ok)
  .Set skins &listing ( $+ $damp.skinlimit $+ ): damp.skinlimit
  .-
  . &Tick rate ( $+ $damp.tickrate $+ ms.): damp.tickrate 
  .-
  .&News: damp.connect
  .U&pdate script: damp.download version
  .&Get lastest skins: damp.download skin
  .-
  . $iif(!$isfile($scriptdirreadme.txt),$style(2)) Readme.txt: run notepad $scriptdirreadme.txt 
  . $iif(!$isfile($scriptdirupdate.txt), $style(2)) Update.txt: run notepad $scriptdirUpdate.txt 
  .-
  .&Unload: unset %damp* | cmd.button.exit | unload -rs $script
}

alias damp.saveskin {
  if (!$isdir($scriptdirsave)) { mkdir $scriptdirsave }
  hsave -io damp.cache $scriptdirsave\saved_skin_ $+ $replace($date,/,_) $+ .ini
}
alias -l listskin {
  if (!$1) { return }
  if ($1 == begin) { window -hl @_ | var %v $findfile($skindir,*.dsf,0,1,aline @_ $$1-) | window -lh @listskin | filter -wwa @_ @listskin }
  if ($1 <= $line(@listskin,0)) && ($1 <= $damp.skinlimit) { return $iif(($shortfn($line(@listskin,$1)) == %damp.lastskin),$style(2)) & $+ $nopath($line(@listskin,$1)) : damp $nopath($shortfn($line(@listskin,$1))) }
  if ($1 == end) { if ($line(@listskin,0) > $damp.skinlimit) { var %a More skins... : listallskin } | window -c @listskin | window -c @_ | return %a }
}
alias -l listallskin {
  if (!$dialog(damp.skins)) { dialog -am damp.skins damp.skins } 
  else { did -r damp.skins 1 | did -ar damp.skins 4 $skindir }
  var %time $ticks
  ;  window -c @damp.skins
  ;  window -ladoCk0 +Lbt @Damp.skins -1 -1 300 200 tahoma 10
  window -hl @_ | var %v $findfile($skindir,*.dsf,0,1,aline @_ $$1-) |  window -lh @damp.listskin | filter -wwa @_ @damp.listskin
  var %i $line(@damp.listskin,0) , %t %i
  while (%i) {
    did -i damp.skins 1 1 $nopath($line(@damp.listskin,%i))
    if ($shortfn($line(@damp.listskin,%i)) == %damp.lastskin) { did -c damp.skins 1 1 }
    dec %i
  }
  window -c @_
  window -c @damp.listskin
  %time = $calc(($ticks - %time) /1000)
  dialog -t damp.skins %t skins listed in %time sec.
}
menu @damp.skins {
  dclick: damp $sline(@damp.skins,1)
}
menu @damp.skins {
  Set skins directory: %damp.skindir = $$sdir($skindir,Select directory,Ok) | listallskin
}
alias -l skindir return $iif(%damp.skindir,%damp.skindir,$scriptdirskins\)
alias musicdir return $iif(%damp.musicdir,$ifmatch,$scriptdirplaylist\)
alias damp {
  var %s = $iif($1-,$skindir $+ \ $+ $1-,%damp.lastskin)
  if ($1 == -s) || (!%s) || (!$isfile(%s)) {
    var %s $$sfile($skindir $+ \,Select a skin,Ok)
  }
  if ($gettok(%s,-1,46) != dsf) { return }
  .window -hl @damp.cache.cmd
  .loadbuf @damp.cache.cmd $f(%s)
  %damp.lastskin = $shortfn(%s)
  if ($hget(damp.cache)) { 
    var %p = $rd(playingline) , %r $rd(random) , %e $rd(repeat)
    hfree damp.cache 
  }
  var %i $line(@damp.cache.cmd,0)
  while (%i) { 
    var %v = [ [ $line(@damp.cache.cmd,%i) ] ] 
    if ($isalias(%v)) { %v }
    dec %i
  }
  window -c @damp.cache.cmd
  if (%p) { v playingline %p }
  v random %r
  v repeat %e
  damp.gui
}
alias -l damp.gui {
  if (!$hget(damp.cache)) { return }
  if (!$rd(w)) || (!$rd(h)) { return }
  .window -apk0CBdo +deL $w -1 -1 $rd(w) $rd(h)
  drawrect -frn $w $c1 1 0 0 $rd(w) $rd(h)
  var %b $button.style | button.style 
  dbox1 0 0 $rd(w) $rd(h)
  cmd.build.pl -b
  button.style %b
  var %i $hget(damp.cache,0).item
  while (%i) {
    var %v = $hget(damp.cache,%i).item
    var %cmd = build. [ $+ [ %v ] ] 
    if ($isalias(%cmd)) { %cmd }
    dec %i
  }
  if ($insong) { damp.tick $insong.fname }
  if (%damp.musiclist) { 
    if  (!$window(@damp.cache)) { build.playlist + %damp.musiclist | cmd.build.pl -s }
    else { cmd.build.pl -s }
  }
  if (%damp.dockwin) { damp.dockwin }
  if (!$insong) { cmd.button.stop }
  dd | ddl
}
alias -l damp.dockwin {
  if ($window(@damp.playlist)) {

    if ($rd(dock.place) == 2) {
      window @damp.playlist $window(@damp).x $calc($window(@damp).y - $window(@damp.playlist).h)
    }

    elseif ($rd(dock.place) == 4) {
      window @damp.playlist $calc($window(@damp).x - $window(@damp).w) $window(@damp).y 
    }

    elseif ($rd(dock.place) == 3) {
      window @damp.playlist $calc($window(@damp).x + $window(@damp).w) $window(@damp).y 
    }

    else {
      window @damp.playlist $window(@damp).x $calc($window(@damp).y + $window(@damp).h)
    }

  }

}
alias -l v hadd -m damp.cache $1 $2-
alias -l uv if ($hget(damp.cache,$1)) { hdel damp.cache $1 }
alias -l rd if ($hget(damp.cache,$1)) { return $ifmatch }
alias -l w return @damp
alias -l f return $+(",$shortfn($1),")
alias -l dd if ($window(@damp)) { drawdot @damp }

alias -l c1 if ($isid) { return $iif($rd(c1),$rd(c1),$rgb(125,125,125)) } | else { v c1 $1- }
alias -l c2 if ($isid) { return $iif($rd(c2),$rd(c2),$rgb(192,192,192)) } | else { v c2 $1- }
alias -l c3 if ($isid) { return $iif($rd(c3),$rd(c3),$rgb(0,0,0)) } | else { v c3 $1- }
alias -l c4 if ($isid) { return $iif($rd(c4),$rd(c4),$rgb(0,0,0)) } | else { v c4 $1- }
alias -l c5 if ($isid) { return $iif($rd(c5),$rd(c5),$rgb(255,255,255)) } | else { v c5 $1- }
alias -l c6 {
  if ($isid) {
    if (!$rd(c6)) { return $c3 }
    return $rd(c6)
  }
  else { v c6 $1- }
}

alias -l window.w if ($isid) { return $rd(w) } | else { v w $1- }
alias -l window.h if ($isid) { return $rd(h) } | else { v h $1- }

alias -l button.style if ($isid) { return $rd(button.style) } | else { v button.style $iif(($1 isin 3d flat static),$1,3d) }

alias -l button.play if ($isid) { return $rd(button.play) } | else { v button.play $1- }
alias -l button.stop if ($isid) { return $rd(button.stop) } | else { v button.stop $1- }
alias -l button.pause if ($isid) { return $rd(button.pause) } | else { v button.pause $1- }
alias -l button.next if ($isid) { return $rd(button.next) } | else { v button.next $1- }
alias -l button.prev if ($isid) { return $rd(button.prev) } | else { v button.prev $1- }
alias -l button.eject if ($isid) { return $rd(button.eject) } | else { v button.eject $1- }
alias -l button.playlist if ($isid) { return $rd(button.playlist) } | else { v button.playlist $1- }
alias -l button.random if ($isid) { return $rd(button.random) } | else { v button.random $1- }
alias -l button.repeat if ($isid) { return $rd(button.repeat) } | else { v button.repeat $1- }

alias -l button.exit if ($isid) { return $rd(button.exit) } | else { v button.exit $1- }
alias -l button.min if ($isid) { return $rd(button.min) } | else { v button.min $1- }

alias -l box.time if ($isid) { return $rd(box.time) } | else { v box.time $1- }
alias -l box.song if ($isid) { return $rd(box.song) } | else { v box.song $1- }
alias -l box.kbs if ($isid) { return $rd(box.kbs) } | else { v box.kbs $1- }
alias -l box.khz if ($isid) { return $rd(box.khz) } | else { v box.khz $1- }
alias -l box.mode if ($isid) { return $rd(box.mode) } | else { v box.mode $1- }
alias -l box.title if ($isid) { return $rd(box.title) } | else { v box.title $1- }

alias -l text.time if ($isid) { return $rd(text.time) } | else { v text.time $1- }
alias -l text.song if ($isid) { return $rd(text.song) } | else { v text.song $1- }
alias -l text.kbs if ($isid) { return $rd(text.kbs) } | else { v text.kbs $1- }
alias -l text.khz if ($isid) { return $rd(text.khz) } | else { v text.khz $1- }
alias -l text.status if ($isid) { return $rd(text.status) } | else { v text.status $1- }
alias -l text.title if ($isid) { return $rd(text.title) } | else { v text.title $1- }

alias -l trackbar.progress if ($isid) { return $rd(trackbar.progress) } | else v trackbar.progress $1-
alias -l trackbar.volume if ($isid) { return $rd(trackbar.volume) } | else v trackbar.volume $1-
alias -l trackbar.progress.percent if ($isid) { return $rd(trackbar.progress.percent) } | else v trackbar.progress.percent $1-
alias -l trackbar.volume.percent if ($isid) { return $rd(trackbar.volume.percent) } | else v trackbar.volume.percent $1-

alias -l random if ($isid) { return $rd(random) } | else v random $1-
alias -l repeat if ($isid) { return $rd(repeat) } | else v repeat $1-
alias -l pause if ($isid) { return $rd(pause) } | else v pause $1-

;trackbar code
alias -l build.trackbar.progress {
  var %cood $rd(trackbar.progress)
  if (!%cood) { return }
  tokenize 44 %cood
  update.trackbar.progress trackbar.progress
}
alias -l build.trackbar.volume {
  var %cood $rd(trackbar.volume)
  if (!%cood) { return }
  tokenize 44 %cood
  drawrect -frn $w $c2 1 $1- 
  dbox2 $1-
  trackbar.volume.percent $calc($vol(wave) / 65536 *100)
  update.trackbar.progress trackbar.volume
}

alias -l update.trackbar.progress {
  var %cood $rd($1) , %percent , %win $w
  if ($1 == trackbar.progress) { %percent = $trackbar.progress.percent }
  if ($1 == trackbar.volume) { %percent = $trackbar.volume.percent }
  if ($1 == pl.scroller) { %percent = $playlist.scroll.percent | %win = $wl }
  if (%percent >= 100) { %percent = 100 }
  if (%percent < 0) { %percent = 0 }
  if (!%cood) { return }
  tokenize 44 %cood
  var  %x $calc(%percent / 100 * $3 * (1- $5) + $1)   , %h $calc($4 /5 *3 +1) , %w $calc($5 * $3 +1) , %y $calc(%h / 3 + $2)
  if (%percent == 0) { inc %x }
  if ($calc(%x  + %w) > $calc($1 + $3)) { %x = $calc(%x - (%x + %w - $3)) }
  if ($3 >= $4) {
    ;draw the trackbar - horizontal
    if (!$6) {
      drawrect -frn %win $c2 1 $1-
      dbox2 %win $1-
      drawrect -frn %win $c1 1 %x %y %w %h
      dbox1 %win %x %y %w %h tr 
    }
    else {
      drawrect -frn %win $c1 1 $1 $calc($2 - %h) $3 $calc(%h *2.8 +2)
      drawrect -frn %win $c2 1 $1-
      dbox2 %win $1-
      drawrect -frn %win $c1 1 %x $calc(%y - %h) %w $calc(%h *2.8)
      dbox1 %win %x $calc(%y - %h) %w $calc(%h *2.8)
    }
  }
  elseif ($4 > $3) {
    var %w $calc($3 / 5 *3) , %h $calc($4 * $5) , %x $calc($1 + %w /3) , %y $calc(($2 + $4) - %percent /100 * $4)
    if (%win == $wl) { %y =  $calc($2 + %percent / 100 * $4) }
    if ($calc(%y  + %h) > $calc($4 + $2)) { %y = $calc($4 + $2 - %h) }
    if (!$6) { 
      drawrect -frn %win $c2 1 $1-
      dbox2 %win $1-
      drawrect -frn %win $c1 1 %x %y %w %h
      dbox1 %win %x %y %w %h tr 
    }
    else {
      drawrect -frn %win $c1 1 $calc($1 - %w) $2 $calc(%w *3.5 +1) $4
      drawrect -frn %win $c2 1 $1-
      dbox2 %win $1-
      drawrect -frn %win $c1 1 $calc(%x -%w) %y $calc(%w *3.5) %h
      dbox1 %win $calc(%x -%w) %y $calc(%w *3.5) %h
    }
  }
  if (%win == $w) { dd }
  ;  else { dd }
}

;draw blank boxes
alias -l dbox1 {
  var %w = $iif(($1 == $wl),$wl,$w) | if ($1 isin $w $wl) { tokenize 32 $2-$6 }
  if ($5 == tr) || ($button.style == 3D) || (!$button.style) {
    drawline -rn %w $c5 1 $1 $calc($2 + $4 -1) $1 $2 $calc($1 + $3 -1) $2
    drawline -rn %w $c4 1 $1 $calc($2 + $4 -1) $calc($1 + $3 -1) $calc($2 + $4 -1) $calc($1 + $3 -1) $2  
  }
  elseif ($button.style == flat) {
    drawrect -rn %w $c1 1 $1 $2 $3 $4
  }
  elseif ($button.style == static) {
    drawrect -rn %w $c4 1 $1-4
    tokenize 32 $calc($1 +1) $calc($2 +1) $calc($3 -1) $calc($4 -1)
    drawline -rn %w $c5 1 $1 $calc($2 + $4 -1) $1 $2 $calc($1 + $3 -1) $2
    drawline -rn %w $c4 1 $1 $calc($2 + $4 -1) $calc($1 + $3 -1) $calc($2 + $4 -1) $calc($1 + $3 -1) $2  
  }
}

alias -l dbox2 {
  var %w = $iif(($1 == $wl),$wl,$w) | if ($1 isin $w $wl) { tokenize 32 $2-$5 }
  if ($button.style isin 3D flat) || (!$button.style) {
    ; drawrect -rn %w $c4 1 $1-4
    ;  tokenize 32 $calc($1 +1) $calc($2 +1) $calc($3 -1) $calc($4 -1)
    ;  drawline -rn %w $c4 1 $1 $calc($2 + $4 -1) $1 $2 $calc($1 + $3 -1) $2
    ;  drawline -rn %w $c5 1 $1 $calc($2 + $4 -1) $calc($1 + $3 -1) $calc($2 + $4 -1) $calc($1 + $3 -1) $2  
    drawline -rn %w $c4 1 $1 $calc($2 + $4 -1) $1 $2 $calc($1 + $3 -1) $2
    drawline -rn %w $c5 1 $1 $calc($2 + $4 -1) $calc($1 + $3 -1) $calc($2 + $4 -1) $calc($1 + $3 -1) $2  
  }
  elseif ($button.style == static) {
    drawrect -rn %w $c4 1 $1-4
    tokenize 32 $calc($1 +1) $calc($2 +1) $calc($3 -1) $calc($4 -1)
    drawline -rn %w $c4 1 $1 $calc($2 + $4 -1) $1 $2 $calc($1 + $3 -1) $2
    drawline -rn %w $c5 1 $1 $calc($2 + $4 -1) $calc($1 + $3 -1) $calc($2 + $4 -1) $calc($1 + $3 -1) $2  
  }
}
alias -l dbox3 {
  var %w = $iif(($1 == $wl),$wl,$w) | if ($1 isin $w $wl) { tokenize 32 $2-$5 }
  drawline -rn %w $c5 1 $1 $calc($2 + $4 -1) $1 $2 $calc($1 + $3 -1) $2
  drawline -rn %w $c4 1 $1 $calc($2 + $4 -1) $calc($1 + $3 -1) $calc($2 + $4 -1) $calc($1 + $3 -1) $2  
}

;build buttons
alias -l build.button.play {
  var %cood $rd(button.play) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  dbox1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(marlett,$5,$1,$2,$3,$4,8) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
} 
alias -l build.button.stop {
  var %cood $rd(button.stop) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(marlett,$5, $1,$2,$3,$4,g) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias -l build.button.pause {
  var %cood $rd(button.pause) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $iif($pause,$c5,$c6) $button.text(webdings,$5, $1,$2,$3,$4,;) }
  if ($7) { drawtext -rcn $w $iif($pause,$c5,$c6) $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias -l build.button.next {
  var %cood $rd(button.next) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(webdings,$5, $1,$2,$3,$4,:) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias -l build.button.prev {
  var %cood $rd(button.prev) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(webdings,$5, $1,$2,$3,$4,9) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias build.button.eject {
  var %cood $rd(button.eject) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(marlett,$5, $1,$2,$3,$4 ,7) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias -l build.button.playlist {
  var %cood $rd(button.playlist) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(wingdings,$5, $1,$2,$3,$4,2) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias -l build.button.random {
  var %cood $rd(button.random) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $iif($random,$c5,$c6) $button.text(tahoma,$5, $1,$2,$3,$4,ø) }
  if ($7) { drawtext -rcn $w $iif($random,$c5,$c6) $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}
alias -l build.button.repeat {
  var %cood $rd(button.repeat) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $iif($repeat,$c5,$c6) $button.text(webdings,$5, $1,$2,$3,$4,`) }
  if ($7) { drawtext -rcn $w $iif($repeat,$c5,$c6( $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}

alias -l build.button.exit {
  var %cood $rd(button.exit) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(marlett,$5, $1,$2,$3,$4,r) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}

alias -l build.button.min {
  var %cood $rd(button.min) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c1 1 $1-4
  if (!$7) { drawtext -rcn $w $c6 $button.text(tahoma,$5, $1,$2,$3,$4,_) }
  if ($7) { drawtext -rcn $w $c6 $button.text($6,$5,$1,$2,$3,$4,$7-) }
  dbox1 $1-
}

;build boxes
alias -l build.box.time var %cood $rd(box.time) | if (!%cood) { return } | draw.text.time
alias -l build.box.song var %cood $rd(box.song) | if (!%cood) { return } | draw.text.song
alias -l build.box.kbs var %cood $rd(box.kbs) | if (!%cood) { return } | draw.text.kbs
alias -l build.box.khz var %cood $rd(box.khz) | if (!%cood) { return } | draw.text.khz
alias -l build.box.mode var %cood $rd(box.mode) | if (!%cood) { return } | draw.text.status
alias -l build.box.title var %cood $rd(box.title) | if (!%cood) { return } | draw.text.title

alias -l button.text { 
  var %t $7-
  if (!$7) { %t = - }
  var %w $calc($width(%t,$1,$2)), %h $height(%t,$1,$2) , %l ,%y 
  if (%h > $6) { %h = $calc($6 -8) } 
  %y = $calc($4 + (($6 - %h) /2)) 
  if (%w > $5) { return $1 $2 $calc($3 +4) %y $calc($5 +8) %h $wrap(%t,$1,$2,$calc($5 - $width(...,$1,$2)),1) $+ ... }
  %l = $calc($3 + (($5 - %w) /2)) 
  return $1 $2 %l %y %w %h $iif(%t,%t,-)
} 

;draw boxes commands
alias -l draw.text.time {
  var %cood $rd(box.time) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c2 1 $1 $2 $3 $4 | dbox2 $1-4 | drawtext -rcn $w $c3 $button.text($5,$6, $1,$2,$3,$4,$rd(text.time)) 
}
alias -l draw.text.song {
  var %cood $rd(box.song) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c2 1 $1 $2 $3 $4 |  dbox2 $1-4 
  var %h $height(T,$5,$6) 
  if (%h > $4) { %h = $calc($4 -8) } 
  if ($rd(scroll)) { drawtext -rcn $w $c3 $5 $6  $calc($1 +2) $calc($2 + (($4 - %h) /2)) $calc($3 - 2 - $witdh(Y,$5,$6)) $4 $rd(text.song) } 
  else { drawtext -rcn $w $c3 $button.text($5,$6, $1,$2,$3,$4,$rd(text.song)) } 
}
alias -l draw.text.kbs {
  var %cood $rd(box.kbs) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c2 1 $1 $2 $3 $4 |  dbox2 $1-4 | drawtext -rcn $w $c3 $button.text($5,$6, $1,$2,$3,$4, $rd(text.kbs)) 
}
alias -l draw.text.khz {
  var %cood $rd(box.khz) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c2 1 $1 $2 $3 $4 |  dbox2 $1-4 | drawtext -rcn $w $c3 $button.text($5,$6, $1,$2,$3,$4, $rd(text.khz)) 
}
alias -l draw.text.status {
  var %cood $rd(box.mode) | if (!%cood) { return } | tokenize 44 %cood  
  drawrect -frn $w $c2 1 $1 $2 $3 $4 | dbox2 $1-4 | drawtext -rcn $w $c3 $button.text($5,$6, $1,$2,$3,$4, $rd(text.status)) 
}
alias -l draw.text.title {
  var %cood $rd(box.title) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $w $c2 1 $1 $2 $3 $4 | dbox2 $1-4 | drawtext -rcn $w $c3 $button.text($5,$6, $1,$2,$3,$4, $rd(text.title)) 
}

;ticker
alias -l damp.tick {
  v tick $1
  v tickname $replace($iif($sound($1).artist,$ifmatch - ) $iif($sound($1).title,$ifmatch,$nopath($longfn($insong.fname))),$chr(32),$chr(160))
  if ($gettok($rd(tickname),-1,46) == mp3) { v tickname $gettok($rd(tickname),$calc($numtok($rd(tickname),46) -1),46) }
  v ticklength $sound($1).length
  text.kbs $sound($1).bitrate kbs
  text.status $sound($1).mode
  text.khz $int($calc($sound($1).sample /1000)) khz
  draw.text.kbs
  draw.text.status
  draw.text.khz
  tokenize 44 $box.song
  if ($5 == terminal) { v tickname $replace($rd(tickname),$chr(160),$chr(255)) }
  if ($width($rd(tickname),$5,$6) >= $calc($gettok($box.song,3,44) -2)) { v scroll 1 | v tickname $rd(tickname) *** $+ $iif(($5 == terminal),$chr(255),$chr(160)) }
  else { v scroll 0 | text.song $rd(tickname) | draw.text.song }
  pl.update.list
  .timerticker -om 0 $damp.tickrate damp.ticker
}

alias -l damp.tickrate {
  if ($isid) { return $iif(%damp.tickrate,$ifmatch,200) }
  var %v $input(Enter tickrate. (Min 200. Max. 1000) ,e, Damp tickrate , $iif(%damp.tickrate,$ifmatch,200))
  if (!%v) { return }
  if (%v < 200) { %v = 200 }
  if (%v > 1000) { %v = 1000 }
  %damp.tickrate = %v
  .timerticker -om 0 %v damp.ticker
}
alias -l damp.skinlimit {
  if ($isid) { return $iif(%damp.skinlimit,$ifmatch,10) }
  var %v $input(Enter maximum skin listing ,e, Damp skinlist , $iif(%damp.skinlimit,$ifmatch,200))
  %damp.skinlimit = %v
}
alias -l damp.ticker {
  if (!$rd(trackbar.progress.ignore)) {
    trackbar.progress.percent $int($calc($insong.pos / $rd(ticklength) * 100))
  }  
  update.trackbar.progress trackbar.progress
  if ($insong) {
    if (!$rd(time)) {
      text.time $t($insong.pos)
    }
    elseif ($rd(time)) {
      text.time - $+ $t($calc($rd(ticklength) - $insong.pos))
    }
  }
  if ($rd(scroll)) { 
    inc %damp.scroll
    if (%damp.scroll > $len($rd(tickname))) { %damp.scroll = 0 }
    text.song $right($rd(tickname), - $+ %damp.scroll) $+ $rd(tickname)
    draw.text.song
  }
  draw.text.time
  dd
}

alias t {
  var %m $int($calc($1 / 1000 / 60)) , %s $int($calc($1 / 1000 - %m * 60))
  return $+(%m,:,$iif((%s <= 9),0 $+ %s,%s))
}

;buttons cmd commands
alias -l cmd.button.play {
  if ($pause) { pause 0 | build.button.pause | .splay resume | dd | return }
  var %f $shortfn($gettok($line(@damp.cache,$sline(@damp.cache,1).ln),1,38))
  if (!$isfile(%f)) { return $cmd.button.next }
  if ($window(@damp.list)) { sline @damp.list $sline(@damp.cache,1).ln }
  v playingline $sline(@damp.cache,1).ln
  .splay stop
  .splay %f
  .damp.tick %f
}
alias -l cmd.button.stop {
  .splay stop
  text.time --:--
  text.song Damp - Creativity to the max!
  tokenize 44 $box.song
  v tickname $replace($text.song *** $+ $chr(32),$chr(32),$iif(($5 == terminal),$chr(255),$chr(160)))
  text.kbs -
  text.khz -
  text.status -
  draw.text.time
  draw.text.song
  draw.text.kbs
  draw.text.khz
  draw.text.status
  trackbar.progress.percent 0
  update.trackbar.progress trackbar.progress
}
alias -l cmd.button.pause {
  if ($pause) { pause 0 | build.button.pause | .splay resume }
  else {
    pause 1 
    build.button.pause
    .splay pause
  }
}
alias -l cmd.button.next {
  if (!$window(@damp.cache)) { return }
  var %s = $iif($rd(playingline),$ifmatch,$sline(@damp.cache,1).ln) , %c %s
  inc %s
  sline @damp.cache %s
  if (!$line(@damp.cache,%s)) {
    if ($repeat) { sline @damp.cache 1 }
    else { return $cmd.button.stop }
  }
  if ($random) && ($line(@damp.cache,0) > 2) { 
    var %r $rand(1,$line(@damp.cache,0)) 
    while (%c == %r) {
      %r = $rand(1,$line(@damp.cache,0)) 
    }
    sline @damp.cache %r
  }
  cmd.button.play
}
alias -l cmd.button.prev {
  if (!$window(@damp.cache)) { return }
  var %s = $iif($rd(playingline),$ifmatch,$sline(@damp.cache,1).ln) , %c %s
  dec %s
  if (!%s) { %s = $line(@damp.cache,0) }
  if (!%s) { return }
  sline @damp.cache %s
  if (!$line(@damp.cache,%s)) {
    if ($repeat) { sline @damp.cache $line(@damp.cache,0) }
    else { return }
  }
  if ($random) && ($line(@damp.cache,0) > 2) { 
    var %r $rand(1,$line(@damp.cache,0)) 
    while (%c == %r) {
      %r = $rand(1,$line(@damp.cache,0)) 
    }
    sline @damp.cache %r
  }
  cmd.button.play
}

alias -l cmd.button.eject {
  build.playlist + $$sfile($musicdir,Select a file,Ok)
  sline @damp.cache $line(@damp.cache,0)
  cmd.button.play
}
alias -l cmd.button.playlist ref.playlist
alias -l cmd.button.random random $iif($random,0,1) | build.button.random
alias -l cmd.button.repeat repeat $iif($repeat,0,1) | build.button.repeat
alias -l cmd.button.exit { 
  cmd.button.stop
  if ($hget(damp.cache)) { hfree damp.cache }
  var  %i $window(@damp*,0)
  while (%i) {
    window -c $window(@damp*,%i)
    dec %i
  }
  .timerticker off 
}
alias -l cmd.button.min if ($window($w)) { window -uh $w | window -w $w | if ($window(@damp.playlist)) { cmd.pl.exit } }

;mouse
menu @damp {
  mouse: if (%damp.dockwin) { damp.dockwin } | return $mo($mouse.x,$mouse.y)
  sclick:return $sc($mouse.x,$mouse.y)
  uclick: return $uc($mouse.x,$mouse.y)
  leave: return $mo($mouse.x,$mouse.y)
}
menu @damp {
  Dock
  .$iif((($rd(dock.place) == 1) || (!$rd(dock.place))) && (%damp.dockwin),$style(1)) Bottom: v dock.place 1 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  .$iif(($rd(dock.place) == 2) && (%damp.dockwin),$style(1)) Top: v dock.place 2 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  .$iif(($rd(dock.place) == 3) && (%damp.dockwin),$style(1)) Right: v dock.place 3 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  .$iif(($rd(dock.place) == 4) && (%damp.dockwin),$style(1)) Left: v dock.place 4 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  -
  &Skin
  .&Select skin: listallskin
  .-
  .$submenu($listskin($1))
  Set skins &listing ( $+ $damp.skinlimit $+ ): damp.skinlimit
  -
  &Tick rate ( $+ $damp.tickrate $+ ms.): damp.tickrate 
  -
  $iif($rd(random),$style(1)) &Random play: v random $iif($rd(random),0,1)
  $iif($rd(repeat),$style(1)) R&epeat play: v repeat $iif($Rd(repeat),0,1)
  -
  Sear&ch playlist: damp.search
}

alias -l g if ($gettok($1,1-4,44)) { return $ifmatch } | else return 0,0,0,0
alias -l p return $replace($g($1),$chr(44),$chr(32))
alias -l h {
  var %h $rd(history)
  if (!%h) { v history $1- | return }
  if (%h == $1-) { return }
  if ($numtok(%h,32) == 4) { dbox1 %h }
  v history $1-
  dd
}

alias -l mo {
  var %x $1 , %y $2
  if (!%y) || (!%x) { return }
  if ($mouse.key & 1) {
    if ($inrect(%x,%y , [ [ $g($button.next) ] ] )) { dbox2 $p($button.next) | h $p($button.next) }
    if ($inrect(%x,%y , [ [ $g($button.prev) ] ] )) { dbox2 $p($button.prev) | h $p($button.prev) }
    if ($inrect(%x,%y , [ [ $g($button.play) ] ] )) { dbox2 $p($button.play) | h $p($button.play) }
    if ($inrect(%x,%y , [ [ $g($button.pause) ] ] )) { dbox2 $p($button.pause) | h $p($button.pause) }
    if ($inrect(%x,%y , [ [ $g($button.stop) ] ] )) { dbox2 $p($button.stop) | h $p($button.stop) }
    if ($inrect(%x,%y , [ [ $g($button.eject) ] ] )) { dbox2 $p($button.eject) | h $p($button.eject) }
    if ($inrect(%x,%y , [ [ $g($button.playlist) ] ] )) { dbox2 $p($button.playlist) | h $p($button.playlist) }
    if ($inrect(%x,%y , [ [ $g($button.random) ] ] )) { dbox2 $p($button.random) | h $p($button.random) }
    if ($inrect(%x,%y , [ [ $g($button.repeat) ] ] )) { dbox2 $p($button.repeat) | h $p($button.repeat) }
    if ($inrect(%x,%y , [ [ $g($button.exit) ] ] )) { dbox2 $p($button.exit) | h $p($button.exit) }
    if ($inrect(%x,%y , [ [ $g($button.min) ] ] )) { dbox2 $p($button.min) | h $p($button.min) }
    if ($inrect(%x,%y, [ [ $g($trackbar.progress) ] ] )) { 
      tokenize 44 $trackbar.progress
      trackbar.progress.percent $calc(($iif(($3 >= $4), %x - $1,%y - $2 -7)) / $iif(($3 >= $4),$3,$4) * 100) 
      update.trackbar.progress trackbar.progress 
      v trackbar.progress.ignore 1
      return
    }
    if ($inrect(%x,%y, [ [ $g($trackbar.volume) ] ] )) { 
      tokenize 44 $trackbar.volume
      var %percent = $calc(($iif(($3 >= $4), %x - $1,%y - $2 -7)) / $iif(($3 >= $4),$3,$4) * 100) 
      if ($3 < $4) { %percent = $calc(100 - %percent) }
      trackbar.volume.percent %percent
      update.trackbar.progress trackbar.volume
      vol -p $calc(65536 * %percent / 100)
      return
    }
  }
  elseif ($button.style == flat) {
    if ($inrect(%x,%y , [ [ $g($button.next) ] ] )) { dbox3 $p($button.next) | h $p($button.next) }
    elseif ($inrect(%x,%y , [ [ $g($button.prev) ] ] )) { dbox3 $p($button.prev) | h $p($button.prev) }
    elseif ($inrect(%x,%y , [ [ $g($button.play) ] ] )) { dbox3 $p($button.play) | h $p($button.play) }
    elseif ($inrect(%x,%y , [ [ $g($button.pause) ] ] )) { dbox3 $p($button.pause) | h $p($button.pause) }
    elseif ($inrect(%x,%y , [ [ $g($button.stop) ] ] )) { dbox3 $p($button.stop) | h $p($button.stop) }
    elseif ($inrect(%x,%y , [ [ $g($button.eject) ] ] )) { dbox3 $p($button.eject) | h $p($button.eject) }
    elseif ($inrect(%x,%y , [ [ $g($button.playlist) ] ] )) { dbox3 $p($button.playlist) | h $p($button.playlist) }
    elseif ($inrect(%x,%y , [ [ $g($button.random) ] ] )) { dbox3 $p($button.random) | h $p($button.random) }
    elseif ($inrect(%x,%y , [ [ $g($button.repeat) ] ] )) { dbox3 $p($button.repeat) | h $p($button.repeat) }
    elseif ($inrect(%x,%y , [ [ $g($button.exit) ] ] )) { dbox3 $p($button.exit) | h $p($button.exit) }
    elseif ($inrect(%x,%y , [ [ $g($button.min) ] ] )) { dbox3 $p($button.min) | h $p($button.min) }
    else { h } 
  }
  else { h }
}
alias -l sc {
  var %x $1 , %y $2
  if (!%y) || (!%x) { return }
  elseif ($inrect(%x,%y, [ [ $g($button.next) ] ] )) { dbox2 $p($button.next) | h $p($button.next) }
  elseif ($inrect(%x,%y, [ [ $g($button.prev) ] ] )) { dbox2 $p($button.prev) | h $p($button.prev) }
  elseif ($inrect(%x,%y, [ [ $g($button.play) ] ] )) { dbox2 $p($button.play) | h $p($button.play) }
  elseif ($inrect(%x,%y, [ [ $g($button.pause) ] ] )) { dbox2 $p($button.pause) | h $p($button.pause) }
  elseif ($inrect(%x,%y, [ [ $g($button.stop) ] ] )) { dbox2 $p($button.stop) | h $p($button.stop) }
  elseif ($inrect(%x,%y, [ [ $g($button.eject) ] ] )) { dbox2 $p($button.eject) | h $p($button.eject) }
  elseif ($inrect(%x,%y, [ [ $g($button.playlist) ] ] )) { dbox2 $p($button.playlist) | h $p($button.playlist) }
  elseif ($inrect(%x,%y, [ [ $g($button.random) ] ] )) { dbox2 $p($button.random) | h $p($button.random) }
  elseif ($inrect(%x,%y, [ [ $g($button.repeat) ] ] )) { dbox2 $p($button.repeat) | h $p($button.repeat) }
  elseif ($inrect(%x,%y, [ [ $g($button.exit) ] ] )) { dbox2 $p($button.exit) | h $p($button.exit) }
  elseif ($inrect(%x,%y, [ [ $g($button.min) ] ] )) { dbox2 $p($button.min) | h $p($button.min) }
  elseif ($inrect(%x,%y, [ [ $g($trackbar.progress) ] ] )) { 
    tokenize 44 $trackbar.progress
    trackbar.progress.percent $calc(($iif(($3 >= $4), %x - $1,%y - $2)) / $iif(($3 >= $4),$3,$4) * 100) 
    update.trackbar.progress trackbar.progress 
    v trackbar.progress.ignore 1
  }
  elseif ($inrect(%x,%y, [ [ $g($trackbar.volume) ] ] )) { 
    tokenize 44 $trackbar.volume
    var %percent = $calc(($iif(($3 >= $4), %x - $1,%y - $2)) / $iif(($3 >= $4),$3,$4) * 100) 
    if ($3 < $4) { %percent = $calc(100 - %percent) }
    trackbar.volume.percent %percent
    update.trackbar.progress trackbar.volume
  }
  elseif ($inrect(%x,%y, [ [ $g($box.title) ] ] )) { 
    window $w $calc($mouse.dx - %x) $calc($mouse.dy - %y) | if ($mouse.key & 1) { .timer@damp -o 1 0 sc %x %y } 
  } 
  dd
}

alias -l uc {
  var %x $1 , %y $2
  if (!%y) || (!%x) { return }
  elseif ($inrect(%x,%y, [ [ $g($box.time) ] ] )) { v time $iif($rd(time),0,1) | h }
  elseif ($inrect(%x,%y, [ [ $g($button.next) ] ] )) { dbox1 $p($button.next) | h $p($button.next) | cmd.button.next }
  elseif ($inrect(%x,%y, [ [ $g($button.prev) ] ] )) { dbox1 $p($button.prev) | h $p($button.prev) | cmd.button.prev }
  elseif ($inrect(%x,%y, [ [ $g($button.play) ] ] )) { dbox1 $p($button.play) | h $p($button.play) | cmd.button.play }
  elseif ($inrect(%x,%y, [ [ $g($button.pause) ] ] )) { dbox1 $p($button.pause) | h $p($button.pause) | cmd.button.pause }
  elseif ($inrect(%x,%y, [ [ $g($button.stop) ] ] )) { dbox1 $p($button.stop) | h $p($button.stop) | cmd.button.stop }
  elseif ($inrect(%x,%y, [ [ $g($button.eject) ] ] )) { dbox1 $p($button.eject) | h $p($button.eject) | cmd.button.eject }
  elseif ($inrect(%x,%y, [ [ $g($button.playlist) ] ] )) { dbox1 $p($button.playlist) | h $p($button.playlist) | cmd.button.playlist }
  elseif ($inrect(%x,%y, [ [ $g($button.random) ] ] )) { dbox1 $p($button.random) | h $p($button.random) | cmd.button.random }
  elseif ($inrect(%x,%y, [ [ $g($button.repeat) ] ] )) { dbox1 $p($button.repeat) | h $p($button.repeat) | cmd.button.repeat }
  elseif ($inrect(%x,%y, [ [ $g($button.exit) ] ] )) { dbox1 $p($button.exit) | h $p($button.exit) | cmd.button.exit }
  elseif ($inrect(%x,%y, [ [ $g($button.min) ] ] )) { dbox1 $p($button.min) | h $p($button.min) | cmd.button.min }
  elseif ($inrect(%x,%y, [ [ $g($trackbar.progress) ] ] )) { 
    tokenize 44 $trackbar.progress
    trackbar.progress.percent $calc(($iif(($3 >= $4), %x - $1,%y - $2)) / $iif(($3 >= $4),$3,$4) * 100) 
    if ($insong) { .splay seek $int($calc($trackbar.progress.percent * $sound($insong.fname).length /100)) }
    update.trackbar.progress trackbar.progress 
  }
  elseif ($inrect(%x,%y, [ [ $g($trackbar.volume) ] ] )) { 
    tokenize 44 $trackbar.volume
    var %percent = $calc(($iif(($3 >= $4), %x - $1,%y - $2)) / $iif(($3 >= $4),$3,$4) * 100) 
    if ($3 < $4) { %percent = $calc(100 - %percent) }
    trackbar.volume.percent %percent
    vol -p $calc(65536 * %percent / 100)
    update.trackbar.progress trackbar.volume
  }
  uv trackbar.progress.ignore 
  update.trackbar.progress trackbar.volume
  h
}
;events
on *:close:@damp: cmd.button.exit
on *:active:@damp: window -oa $w 
on *:active:@damp.cache: window -h @damp.cache
on *:mp3end: cmd.button.next
on *:keydown:@damp:*: {
  var %v $keyval
  if (%v == 90) { cmd.button.prev }
  if (%v == 88) { cmd.button.play }
  if (%v == 67) { cmd.button.pause }
  if (%v == 86) { cmd.button.stop }
  if (%v == 66) { cmd.button.next }
  if (%v == 46) { cmd.button.eject }
  if (%v == 80) { cmd.button.playlist }
  if (%v == 82) { cmd.button.random | dd }
  if (%v == 69) { cmd.button.repeat | dd }
  if (%v == 219) { 
    window -hl @_ | var %v $findfile($skindir,*.dsf,0,1,aline @_ $$1-) | window -lh @listskin | filter -wwa @_ @listskin 
    if (!%damp.skinnumber) { %damp.skinnumber = 2 }
    if (%damp.skinnumber == 1) { %damp.skinnumber = $calc($line(@listskin,0) +1) }
    dec %damp.skinnumber
    damp $nopath($line(@listskin,%damp.skinnumber))
    window -c @listskin
    window -c @_
  }
  if (%v == 221) { 
    window -hl @_ | var %v $findfile($skindir,*.dsf,0,1,aline @_ $$1-) | window -lh @listskin | filter -wwa @_ @listskin 
    if (!%damp.skinnumber) { %damp.skinnumber = 0 }
    if (%damp.skinnumber == $line(@listskin,0)) { %damp.skinnumber = 0 }
    inc %damp.skinnumber
    damp $nopath($line(@listskin,%damp.skinnumber))
    window -c @listskin
    window -c @_
  }
}
on *:keydown:@damp.list:*: {
  if ($keyval == 46) { build.playlist - }
  if ($keyval == 13) { sline @damp.cache $sline(@damp.list,1).ln | cmd.button.play }
}

on *:appactive: {
  if ($window($w)) { 
    if ($appactive) { window -o $w } 
    else { window -u $w } 
  } 
  if ($window($wl)) { 
    if ($appactive) && ($window($wl).state == normal) { window -o +L $wl } 
    else { window -u +L $wl } 
  } 
  if ($window(@damp.list)) {
    if ($appactive) { window -o @damp.list }
    else { window -u @damp.list }
  }
}

on *:close:@damp.playlist: .timer -om 1 0 cmd.build.pl -b | .timer -om 1 1 cmd.build.pl -h

;Damp picwin playlist

alias -l wl return @damp.playlist
alias -l ddl if ($window(@damp.playlist)) { drawdot @damp.playlist }

alias -l playlist.title if ($isid) { return $rd(pl.title) } | else { v pl.title $1- }
alias -l playlist.w if ($isid) { return $rd(pl.w) } | else { v pl.w $1- }
alias -l playlist.h if ($isid) { return $rd(pl.h) } | else { v pl.h $1- }
alias -l playlist.list if ($isid) { return $rd(pl.list) } | else { v pl.list $1- }
alias -l playlist.addsong if ($isid) { return $rd(pl.+song) } | else { v pl.+song $1- }
alias -l playlist.delsong if ($isid) { return $rd(pl.delsong) } | else { v pl.delsong $1- }
alias -l playlist.adddir if ($isid) { return $rd(pl.adddir) } | else { v pl.adddir $1- }
alias -l playlist.addlist if ($isid) { return $rd(pl.addlist) } | else { v pl.addlist $1- }
alias -l playlist.dellist if ($isid) { return $rd(pl.dellist) } | else { v pl.dellist $1- }
alias -l playlist.savelist if ($isid) { return $rd(pl.savelist) } | else { v pl.savelist $1- }
alias -l playlist.deflist if ($isid) { return $rd(pl.deflist) } | else { v pl.deflist $1- }
alias -l playlist.buttonup if ($isid) { return $rd(pl.buttonup) } | else { v pl.buttonup $1- }
alias -l playlist.buttondw if ($isid) { return $rd(pl.buttondw) } | else { v pl.buttondw $1- }
alias -l playlist.scroller if ($isid) { return $rd(pl.scroller) } | else { v pl.scroller $1- } 
alias -l playlist.scroll.percent if ($isid) { return $rd(pl.scroll.percent) } | else { v pl.scroll.percent $1- } 
alias -l playlist.exit if ($isid) { return $rd(pl.exit) } | else { v pl.exit $1- } 

alias -l build.pl.title var %cood $rd(pl.title) | if (!%cood) { return } | draw.text.pl.title
alias -l draw.text.pl.title {
  var %cood $rd(pl.title) | if (!%cood) { return } | tokenize 44 %cood 
  drawrect -frn $wl $c2 1 $1 $2 $3 $4 | dbox2 $wl $1-4 | drawtext -rcn $wl $c3 $button.text($5,$6, $1,$2,$3,$4,$iif($7,$7-,Damp Playlist)) 
  var %h $calc($4 / 5 * 3)
  playlist.exit $calc($1 + $3 -%h -2) $+ , $+ $calc($2 + $4 /5) $+ , $+  %h $+ , $+ %h
  build.pl.exit
}
alias -l build.pl.exit {
  var %cood $rd(pl.exit) | if (!%cood) { return }
  var %h $height(r,marlett,10) , %s 10 , %w 10
  tokenize 44 %cood 
  while (%h < $calc($4 /5 *3)) && (%w < $calc($3 /5 *3)) {
    inc %s | %h = $height(r,marlett,%s) | %w = $width(r,marlett,%s) 
  }
  drawrect -frn $wl $c1 1  $1-4
  dbox1 $wl $1-4 
  drawtext -rcn $wl $c6 $button.text(marlett,%s, $1,$2,$3,$4,r) 
}
alias -l build.pl.list {
  var %cood $rd(pl.list)
  if (!%cood) { return }
  tokenize 44 %cood | drawrect -frn $wl $c2 1 $1 $2 $calc($3 -14) $4 | dbox2 $wl $1 $2 $calc($3 -14) $4
  playlist.buttonup $calc($1 + $3 -12) $+  , $+ $2 $+  , $+ 12,12
  playlist.buttondw $calc($1 + $3 -12) $+  , $+ $calc($2 + $4 -12) $+  , $+ 12,12
  if (!$7) { playlist.scroller $calc($1 + $3 -12) $+  , $+ $calc($2 + 14) $+  , $+ 12 $+  , $+ $calc($4 - 28) , $+ 0.05 }
  else { playlist.scroller $calc($1 + $3 -8) $+ , $+ $calc($2 + 14) $+ , $+ 5 $+ , $+ $calc($4 - 28) $+ , $+ 0.15,1 }
  build.pl.buttonup | build.pl.buttondw | build.pl.scroller
}
alias -l build.pl.buttonup {
  var %cood $rd(pl.buttonup) | if (!%cood) { return } | tokenize 44 %cood
  drawrect -frn $wl $c1 1  $1-4 | dbox1 $wl $1-4 | drawtext -rcn $wl $c6 $button.text(marlett,10, $1,$2,$3,$4,t) 
}
alias -l build.pl.buttondw {
  var %cood $rd(pl.buttondw) | if (!%cood) { return } | tokenize 44 %cood
  drawrect -frn $wl $c1 1  $1-4 | dbox1 $wl $1-4 | drawtext -rcn $wl $c6 $button.text(marlett,10, $1,$2,$3,$4,u) 
}
alias -l build.pl.scroller {
  var %cood $rd(pl.scroller) | if (!%cood) { return } | tokenize 44 %cood
  dbox2 $wl $1-4 
  playlist.scroll.percent 0
  update.trackbar.progress pl.scroller
}
alias build.pl.+song {
  var %cood $rd(pl.+song) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7-,+ song))
  dbox1 $wl $1-4
}
alias -l build.pl.delsong { 
  var %cood $rd(pl.delsong) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7-,- song))
  dbox1 $wl $1-4
}
alias -l build.pl.adddir { 
  var %cood $rd(pl.adddir) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7-,+ dir)) 
  dbox1 $wl $1-4
}
alias -l build.pl.addlist {
  var %cood $rd(pl.addlist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7,+ list))
  dbox1 $wl $1-4
}
alias -l build.pl.dellist { 
  var %cood $rd(pl.dellist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7-,- list)) | dbox1 $wl $1-4
}
alias -l build.pl.savelist { 
  var %cood $rd(pl.savelist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7-,Save)) | dbox1 $wl $1-4
}
alias -l build.pl.deflist {
  var %cood $rd(pl.deflist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c6 $button.text($5,$6,$1,$2,$3,$4,$iif($7,$7-,Default)) | dbox1 $wl $1-4
}

alias -l cmd.pl.+song { build.playlist + $$sfile($musicdir,Select mp3...,Ok) | pl.update.list }
alias -l cmd.pl.delsong { build.playlist $iif(($1 == c),c,-) | pl.update.list }
alias -l cmd.pl.adddir { var %a $findfile($$sdir($musicdir,Select directory,Ok),*.mp3,0,build.playlist + $$1-) | ref.playlist | v scrollline $iif($sline(@damp.cache,1).ln,$ifmatch,1) | pl.update.list }
alias -l cmd.pl.addlist { build.playlist + $$sfile($musicdir,Select playlist...,Ok) | v scrollline $iif($sline(@damp.cache,1).ln,$ifmatch,1) | pl.update.list }
alias -l cmd.pl.dellist { build.playlist - * | v scrollline $iif($sline(@damp.cache,1).ln,$ifmatch,1) | pl.update.list }
alias -l cmd.pl.savelist { save.playlist | v scrollline $iif($sline(@damp.cache,1).ln,$ifmatch,1) | pl.update.list }
alias -l cmd.pl.deflist { var %f = $$sfile($musicdir,Set default playlist...,Ok) | savebuf @damp.cache $f(%f) | %damp.musiclist = %f }
alias -l cmd.pl.exit { window -uh $wl }

alias -l h2 {
  var %h $rd(pl.history)
  if (!%h) { v pl.history $1- | return }
  if (%h == $1-) { return }
  if ($numtok(%h,32) == 4) { dbox1 $wl %h }
  v pl.history $1-
  ddl
}

menu @damp.playlist {
  mouse:return $mo2($mouse.x,$mouse.y)
  sclick:return $sc2($mouse.x,$mouse.y)
  uclick:return $uc2($mouse.x,$mouse.y)
  dclick: return $dc2($mouse.x,$mouse.y)
  leave:return $mo2(4mouse.x,$mouse.y)
}
menu @damp.playlist {
  Dock
  .$iif(($rd(dock.place) == 1) && (%damp.dockwin),$style(1)) Bottom: v dock.place 1 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  .$iif(($rd(dock.place) == 2) && (%damp.dockwin),$style(1)) Top: v dock.place 2 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  .$iif(($rd(dock.place) == 3) && (%damp.dockwin),$style(1)) Right: v dock.place 3 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  .$iif(($rd(dock.place) == 4) && (%damp.dockwin),$style(1)) Left: v dock.place 4 | set %damp.dockwin 1 | if (%damp.dockwin) { damp.dockwin }
  -
  Playlist
  .Save as...: cmd.pl.savelist
  .Load: cmd.pl.addlist
  .-
  .$iif(%damp.autosave,$style(1)) Auto save: %damp.autosave = $iif(%damp.autosave,0,1) | return $damp.autosave
  .-
  .Default: cmd.pl.deflist
  -
  Add
  .File:cmd.pl.+song
  .Dir:cmd.pl.adddir
  - 
  Delete
  .Selected:cmd.pl.delsong
  .All: cmd.pl.delsong c
  -
  Search: damp.search
}

alias -l mo2 {
  var %x $1 , %y $2
  if (!%y) || (!%x) { return }
  if ($mouse.key & 1) {
    if ($inrect(%x,%y, [ [ $g($rd(pl.+song)) ] ] )) { dbox2 $wl $p($rd(pl.+song)) | h2 $p($rd(pl.+song)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.delsong)) ] ] )) { dbox2 $wl $p($rd(pl.delsong)) | h2 $p($rd(pl.delsong)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.addlist)) ] ] )) { dbox2 $wl $p($rd(pl.addlist)) | h2 $p($rd(pl.addlist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.dellist)) ] ] )) { dbox2 $wl $p($rd(pl.dellist)) | h2 $p($rd(pl.dellist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.adddir)) ] ] )) { dbox2 $wl $p($rd(pl.adddir)) | h2 $p($rd(pl.adddir)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.savelist)) ] ] )) { dbox2 $wl $p($rd(pl.savelist)) | h2 $p($rd(pl.savelist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.deflist)) ] ] )) { dbox2 $wl $p($rd(pl.deflist)) | h2 $p($rd(pl.deflist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.exit)) ] ] )) { dbox2 $wl $p($rd(pl.exit)) | h2 $p($rd(pl.exit)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttonup)) ] ] )) { dbox2 $wl $p($rd(pl.buttonup)) | h2 $p($rd(pl.buttonup)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttondw)) ] ] )) { dbox2 $wl $p($rd(pl.buttondw)) | h2 $p($rd(pl.buttondw)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.scroller)) ] ] )) { cmd.pl.scroller %y scroll } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.list)) ] ] )) {
      var %i = $rd(selectedline) , %v = $line(@damp.cache,%i)
      if (!%v) { return }
      var %cood $rd(pl.list)
      tokenize 44 %cood  
      tokenize 32 $1 $2 $calc($3 -14) $4 $5-
      if (!$inrect(%x,%y,$1,$2,$3,$4)) { return }
      var %h $int($calc($height(T,$5,$6) +2)) , %l $int($calc((%y - $2) / %h + $rd(scrollline)))
      if (%l <= 0) { %l = 1 } | if (%l >= $line(@damp.cache,0)) { %l = $line(@damp.cache,0) }
      ;shift button
      elseif ($mouse.key & 4) {
        v shiftline %l
        v shiftlist 1
      }
      else {
        if (%l) { 
          dline @damp.cache %i 
          iline @damp.cache %l %v 
          sline @damp.cache %l
          v selectedline %l
          v scrollline $iif(($calc(%l - 5) > 0), $ifmatch,1)
        }
      }
      pl.update.list
    }
    else { h2 }
  }
  elseif ($button.style == flat) {
    if ($inrect(%x,%y, [ [ $g($rd(pl.+song)) ] ] )) { dbox3 $wl $p($rd(pl.+song)) | h2 $p($rd(pl.+song)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.delsong)) ] ] )) { dbox3 $wl $p($rd(pl.delsong)) | h2 $p($rd(pl.delsong)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.addlist)) ] ] )) { dbox3 $wl $p($rd(pl.addlist)) | h2 $p($rd(pl.addlist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.dellist)) ] ] )) { dbox3 $wl $p($rd(pl.dellist)) | h2 $p($rd(pl.dellist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.adddir)) ] ] )) { dbox3 $wl $p($rd(pl.adddir)) | h2 $p($rd(pl.adddir)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.savelist)) ] ] )) { dbox3 $wl $p($rd(pl.savelist)) | h2 $p($rd(pl.savelist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.deflist)) ] ] )) { dbox3 $wl $p($rd(pl.deflist)) | h2 $p($rd(pl.deflist)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.exit)) ] ] )) { dbox3 $wl $p($rd(pl.exit)) | h2 $p($rd(pl.exit)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttonup)) ] ] )) { dbox3 $wl $p($rd(pl.buttonup)) | h2 $p($rd(pl.buttonup)) } 
    elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttondw)) ] ] )) { dbox3 $wl $p($rd(pl.buttondw)) | h2 $p($rd(pl.buttondw)) } 
    else { h2 }
  }

  ddl
}

alias -l sc2 {
  var %x $1 , %y $2
  if (!%y) || (!%x) { return }
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.+song)) ] ] )) { dbox2 $wl $p($rd(pl.+song)) | h2 $p($rd(pl.+song)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.delsong)) ] ] )) { dbox2 $wl $p($rd(pl.delsong)) | h2 $p($rd(pl.delsong)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.adddir)) ] ] )) { dbox2 $wl $p($rd(pl.adddir)) | h2 $p($rd(pl.adddir)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.addlist)) ] ] )) { dbox2 $wl $p($rd(pl.addlist)) | h2 $p($rd(pl.addlist)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.dellist)) ] ] )) { dbox2 $wl $p($rd(pl.dellist)) | h2 $p($rd(pl.dellist)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.savelist)) ] ] )) { dbox2 $wl $p($rd(pl.savelist)) | h2 $p($rd(pl.savelist)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.deflist)) ] ] )) { dbox2 $wl $p($rd(pl.deflist)) | h2 $p($rd(pl.deflist)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.exit)) ] ] )) { dbox2 $wl $p($rd(pl.exit)) | h2 $p($rd(pl.exit)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttonup)) ] ] )) { dbox2 $wl $p($rd(pl.buttonup)) | h2 $p($rd(pl.buttonup)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttondw)) ] ] )) { dbox2 $wl $p($rd(pl.buttondw)) | h2 $p($rd(pl.buttondw)) } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.scroller)) ] ] )) { cmd.pl.scroller %y scroll } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.list)) ] ] )) {
    var %cood $rd(pl.list)
    tokenize 44 %cood  
    tokenize 32 $1 $2 $calc($3 -14) $4 $5-
    if (!$inrect(%x,%y,$1,$2,$3,$4)) { return }
    var %h $int($calc($height(T,$5,$6) +2)) , %l $int($calc((%y - $2) / %h + $rd(scrollline)))
    ;ctrl button
    if ($mouse.key & 2) {
      v ctrlline $rd(ctrlline) %l
      v ctrllist 1
    }
    ;shift button
    elseif ($mouse.key & 4) {
      v shiftline %l
      v shiftlist 1
    }
    else {
      if (%l <= 0) { %l = 1 } | if (%l >= $line(@damp.cache,0)) { %l = $line(@damp.cache,0) }
      if (%l) { sline @damp.cache %l }
      v selectedline %l
    }
    pl.update.list
  } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.title)) ] ] )) {
    %damp.dockwin = 0
    window $wl $calc($mouse.dx - %x) $calc($mouse.dy - %y) | if ($mouse.key & 1) { .timer@damp -o 1 0 sc2 %x %y } 
  } 
  ddl
}

alias -l uc2 {
  var %x $1 , %y $2
  if (!%y) || (!%x) { return }
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.+song)) ] ] )) { dbox1 $wl $p($rd(pl.+song)) | h2 $p($rd(pl.+song)) | cmd.pl.+song } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.delsong)) ] ] )) { dbox1 $wl $p($rd(pl.delsong)) | h2 $p($rd(pl.delsong)) | cmd.pl.delsong } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.adddir)) ] ] )) { dbox1 $wl $p($rd(pl.adddir)) | h2 $p($rd(pl.adddir)) | cmd.pl.adddir } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.addlist)) ] ] )) { dbox1 $wl $p($rd(pl.addlist)) | h2 $p($rd(pl.addlist)) | cmd.pl.addlist } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.dellist)) ] ] )) { dbox1 $wl $p($rd(pl.dellist)) | h2 $p($rd(pl.dellist)) | cmd.pl.dellist } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.savelist)) ] ] )) { dbox1 $wl $p($rd(pl.savelist)) | h2 $p($rd(pl.savelist)) | cmd.pl.savelist } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.deflist)) ] ] )) { dbox1 $wl $p($rd(pl.deflist)) | h2 $p($rd(pl.deflist)) | cmd.pl.deflist } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.exit)) ] ] )) { dbox1 $wl $p($rd(pl.exit)) | h2 $p($rd(pl.exit)) | cmd.pl.exit } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttonup)) ] ] )) { dbox1 $wl $p($rd(pl.buttonup)) | h2 $p($rd(pl.buttonup)) | cmd.pl.buttonup } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.buttondw)) ] ] )) { dbox1 $wl $p($rd(pl.buttondw)) | h2 $p($rd(pl.buttondw)) | cmd.pl.buttondw } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.scroller)) ] ] )) { cmd.pl.scroller %y scroll } 
  elseif ($inrect(%x,%y, [ [ $g($rd(pl.list)) ] ] )) {
    var %cood $rd(pl.list)
    tokenize 44 %cood  
    tokenize 32 $1 $2 $calc($3 -14) $4 $5-
    if (!$inrect(%x,%y,$1,$2,$3,$4)) { return }
    var %h $int($calc($height(T,$5,$6) +2)) , %l $int($calc((%y - $2) / %h + $rd(scrollline)))
    if (%l <= 0) { %l = 1 } | if (%l >= $line(@damp.cache,0)) { %l = $line(@damp.cache,0) }
    if (%l) { sline @damp.cache %l }
    if ($mouse.key != 4) {
      uv shiftline
      uv shiftlist
    }
    if ($mouse.key != 2) {
      uv ctrlline
      uv ctrllist
    }
    pl.update.list
  } 
  ddl
}

alias -l dc2 {
  var %x $1 , %y $2
  if (!%x) || (!%y) { return }
  var %cood $rd(pl.list) | if (!%cood) { return }
  tokenize 44 %cood  
  tokenize 32 $1 $2 $calc($3 -14) $4 $5-
  if ($inrect(%x,%y,$1,$2,$3,$4)) {
    var %h $int($calc($height(T,$5,$6) +2)) , %l $int($calc((%y - $2) / %h + $rd(scrollline)))
    if (%l <= 0) { %l = 1 } | if (%l >= $line(@damp.cache,0)) { %l = $line(@damp.cache,0) }
    if (%l) { sline @damp.cache %l }
    cmd.button.play   
    pl.update.list
  }
}

alias -l cmd.pl.scroller {
  if (!$1) { return }
  var %cood $rd(pl.scroller) , %y $1 , %v $2
  if (!%cood) { return }
  tokenize 44 %cood 
  var %p $int($calc((%y - $2) / $4 *100))
  v pl.scroll.percent $iif((%p > 95),95,%p)
  update.trackbar.progress pl.scroller
  if (%v) { 
    var %t $int($calc(%p / 100 * $line(@damp.cache,0))) 
    v scrollline $iif((%t <= 0),1,%t)
    pl.update.list
  }
}
alias -l cmd.pl.buttonup {
  var %s $rd(scrollline)
  dec %s
  if (%s <= 0) { %s = 1 }
  v scrollline %s
  pl.update.list
}
alias -l cmd.pl.buttondw {
  var %s $rd(scrollline)
  inc %s
  if (%s >= $line(@damp.cache,0)) { dec %s }
  v scrollline %s
  pl.update.list
}

on *:keydown:@damp.playlist:*: {
  var %k $keyval
  if (%k == 46) { build.playlist - | pl.update.list }
  elseif (%k == 13) { cmd.button.play }
  elseif (%k == 38) { 
    sline @damp.cache $iif($calc($rd(scrollline) -1),$ifmatch,1) 
    v playingline $sline(@damp.cache,1).ln 
    cmd.pl.buttonup 
  }
  elseif (%k == 40) { 
    sline @damp.cache $calc($rd(scrollline) +1) 
    v playingline $sline(@damp.cache,1).ln 
    cmd.pl.buttondw 
  }
}

alias -l pl.update.list {
  var %cood $rd(pl.list) | if (!%cood) { return }
  tokenize 44 %cood
  tokenize 32 $1 $2 $calc($3 -14) $4 $5-
  var %h $int($calc($height(T,$5,$6) +2)) , %l $int($calc($4 / %h)) , %s $rd(scrollline) , %i 1 , %w $calc($3 - $width(T,$5,$6) *2) , %x $calc($1 + $width(T,$5,$6)) , %y $calc($2 + 2)
  drawrect -frn $wl $c2 1 $1-4 | dbox2 $wl $1-4
  while (%i <= %l) {
    var %v 
    if ($gettok($line(@damp.cache,%s),2,38)) { %v = $ifmatch }
    else { %v = $nopath($longfn($gettok($line(@damp.cache,%s),1,38))) }
    if (!%v) { inc %i | continue }
    if ($width(%v,$5,$6) > %w) { %v = $button.text($5,$6,$calc($1 +2.5),%y,%w,%h,%v)  }
    else { %v = $5 $6 %x %y %w %h %v }
    if (%s == $sline(@damp.cache,1).ln) { drawrect -frn $wl $c5 1 %x %y %w %h  }
    if ($rd(ctrllist)) && ($rd(ctrlline)) { 
      var %_c $rd(ctrlline) 
      if ($istok(%_c,%s,32)) { drawrect -frn $wl $c5 1 %x %y %w %h }
    }
    if ($rd(shiftlist)) && ($rd(shiftline)) {
      var %_s $rd(selectedline) , %_e $rd(shiftline) , %_j
      if (%_s > %_e) { %_j = $+(%_e,-,%_s) }
      elseif (%_e > %_s) { %_j = $+(%_s,-,%_e) }      
      %_j =  [  %_j ] ] 
      if (%s isnum %_j) { 
      drawrect -frn $wl $c5 1 %x %y %w %h }
    }
    drawtext -rcn $wl $c3 %v
    inc %i
    inc %s
    inc %y %h
  }
  var %p $int($calc($rd(scrollline) / $line(@damp.cache,0) *100))
  if ($rd(scrollline) == 1) { %p = 0 }
  elseif ($rd(scrollline) == $line(@damp.cache,0)) { %p = 100 }
  v pl.scroll.percent $iif((%p > 100),100,%p)
  update.trackbar.progress pl.scroller
  ddl
}

;playlist code
alias -l build.playlist {
  if ($1 == c) { clear @damp.cache }
  if ($1 == +) || ($1 == c) {
    if (!$window(@damp.cache)) { window -lh @damp.cache }
    var %ext $gettok($nopath($longfn($2-)),-1,46)
    %damp.musicdir = $remove($2-,$gettok($2-,-1,92))
    if (!$isfile($2-)) { return File not found! } 
    if (%ext == mp3) { aline @damp.cache $longfn($2-) }
    ;& $iif($sound($2-).artist,$ifmatch -) $sound($2-).title } 
    else { 
      .window -lh @_
      .loadbuf @_ $f($2- )
      filter -ww @_ @damp.cache *.mp3
      .window -c @_
    }
    if ($window(@damp.list)) { ref.playlist }
    return $damp.autosave
  }
  if ($1 == -) {
    if ($rd(ctrllist)) && ($rd(ctrlline)) {
      var %v = $rd(ctrlline) , %i = $numtok(%v,32)
      while (%i) {
        dline @damp.cache $gettok(%v,%i,32)
        dec %i
      }
      uv ctrlline | uv ctrllist | pl.update.list
    }
    if ($rd(shiftlist)) && ($rd(shiftline)) {
      var %_s $rd(selectedline) , %_e $rd(shiftline) , %_j
      if (%_s > %_e) { %_j = $+(%_e,-,%_s) }
      elseif (%_e > %_s) { %_j = $+(%_s,-,%_e) }      
      var %i = $line(@damp.cache,0)
      while (%i) { 
        if (%i isnum [ [ %_j ] ] ) {
          dline @damp.cache %i
        }
        dec %i
      }
      uv shiftline | uv shiftlist | pl.update.list
    }

    if ($2 == *) { .clear @damp.cache | return $damp.autosave }
    var %i $sline(@damp.list,0)
    if ($window($wl)) { %i = $sline(@damp.cache,0) }
    while (%i) {
      var %l = $sline(@damp.list,%i).ln
      if ($window($wl)) { %l = $sline(@damp.cache,%i).ln }
      if ($window(@damp.list)) { dline @damp.list %l }
      dline @damp.cache %l
      dec %i
    }
    return $damp.autosave
  }
}

alias -l ref.playlist {
  if ($rd(pl.list)) && ($window($wl)) { v scrollline $iif($sline(@damp.cache,1).ln,$ifmatch,1) | pl.update.list | window -doa $wl | return }
  if (!$window(@damp.list)) { window -ladohk0 +tebL @damp.list $mouse.dx $mouse.dy 400 300 }
  clear @damp.list
  var %i $line(@damp.cache,0)
  while (%i) {
    iline @damp.list 1 $nopath($longfn($line(@damp.cache,%i)))
    dec %i
  }
  if ($sline(@damp.cache,1).ln) { sline @damp.list $ifmatch }
}

alias -l save.playlist {
  var %f = $$sfile($musicdir,Select a file,Save)
  savebuf @damp.cache $f(%f)
  %damp.musiclist = %f
}
alias -l damp.autosave {
  if ($isid) && (!%damp.autosave) { return }
  var %f = $scriptdirplaylist\playlist.txt
  if (!$isdir($scriptdirplaylist)) { mkdir $scriptdirplaylist }
  if (!$window(@damp.cache)) { return }
  savebuf @damp.cache $f(%f)
  %damp.musiclist = %f
}
menu @damp.list {
  dclick:sline @damp.cache $sline(@damp.list,1).ln | cmd.button.play 
}
menu @damp.list {
  Playlist
  .Save as...: save.playlist
  .Load: clear @damp.cache | build.playlist + $$sfile($musicdir,Select file,Ok) | ref.playlist
  .-
  .Default: { var %f = $$sfile($musicdir,Save playlist as...,Save) | savebuf @damp.cache $f(%f) | %damp.musiclist = %f }
  -
  Add
  .File:build.playlist + $$sfile($musicdir,Select file,Ok)
  .Dir:window -c @damp.list | var %a $findfile($$sdir($musicdir,Select directory,Ok),*.mp3,0,build.playlist + $$1-) | ref.playlist
  - 
  Delete
  .Selected:build.playlist -
  .All: build.playlist c
}
alias -l cmd.build.pl {
  :b
  if ($playlist.w) && ($playlist.h) && ($1 == -b) { 
    .window -pBCdoh +deL $wl -1 -1 $playlist.w $playlist.h
    drawrect -fr $wl $c1 1 0 0 $playlist.w $playlist.h
    dbox1 $wl 0 0 $playlist.w $playlist.h
    v scrollline $iif($sline(@damp.cache,1).ln,$ifmatch,1)
    build.pl.list 4,26,314,300,tahoma,10
    build.pl.title 4,4,314,20,tahoma,10
    build.pl.+song 4,328,40,20,tahoma,10
    build.pl.delsong 46,328,40,20,tahoma,10
    build.pl.adddir 88,328,40,20,tahoma,10
    build.pl.addlist 130,328,40,20,tahoma,10
    build.pl.dellist 172,328,40,20,tahoma,10
    build.pl.savelist 236,328,40,20,tahoma,10
    build.pl.deflist 278,328,40,20,tahoma,10
  }
  if ($1 == -h) { if (!$window($wl)) { tokenize 32 -b | goto b } | .window -duh +L $wl | pl.update.list }
  if ($1 == -s) { sline @damp.cache $iif($rd(scrollline),$ifmatch,1) | pl.update.list }
}

;Script & Skin updater
alias -l sc.c return damp.socket
alias damp.connect { sockclose $sc.c | sockopen $sc.c www.ileet.net 80 }
on *:sockopen:damp.socket: {
  if ($sockerr) { decho Connection error $+([,$sockerr,],.) | return }
  sockwrite -n $sockname GET http://www.ileet.net/~smallmoskito/damp/news.txt
  sockwrite -n $sockname
  prog 10 Connecting...
}
on *:sockread:damp.socket: {
  if ($sockerr) { decho Connection error $+([,$sockerr,],.) | prog -c | return }
  var %t | sockread %t
  prog 30 Downloading news...   
  while ($sockbr) {
    list %t
    sockread %t
  }
  prog 100 Done.
  sockclose $sockname
  sline @damp.news 10
  window -aw @damp.news
  window -c @prog
}

alias -l list {
  if (!$window(@damp.news)) { window -Chk0do +Lts @damp.news -1 -1 450 300 tahoma 10 }
  echo @damp.news $iif($1-,$1-,$chr(160))
}

alias -l prog {
  if ($1 == -c) { window -c @prog | return }
  var %c1 $iif($hget(damp.cache,c1),$ifmatch,$rgb(face)) 
  var %c2 $iif($hget(damp.cache,c2),$ifmatch,$rgb(face))
  var %c3 $iif($hget(damp.cache,c3),$ifmatch,$rgb(text))
  var %c4 $iif($hget(damp.cache,c4),$ifmatch,$rgb(shadow))
  var %c5 $iif($hget(damp.cache,c5),$ifmatch,$rgb(hilight))
  if (!$window(@prog)) { 
    .window -padoBC +Lde @prog -1 -1 250 30 
    drawrect -frn @prog %c1 1 0 0 250 30
    drawline -rn @prog %c5 1 0 30 0 0 250 0
    drawline -rn @prog %c4 1 0 29 249 29 249 0
    drawrect -rn @prog %c5 1 6 17 240 7
  }
  if ($1 isnum) {
    var %percent $1
    if (%percent >= 100) { %percent = 100 }
    if (%percent < 0) || (!%percent) { %percent = 0 }
    drawrect -frn @prog %c1 1 10 3 235 12
    drawtext -rcon @prog %c3 tahoma 10 10 3 190 12 $2-
    drawtext -crno @prog %c3 tahoma 10  210 3 40 12 %percent $+ %
    drawline -rn @prog %c1 3 10 20 240 20
    drawline -rn @prog %c5 3 10 20 $calc(%percent / 100 * 230 +10) 20
  }
  drawdot @prog
}
menu @prog {
  dclick: prog -c
}
alias damp.download {
  if (!$1) { return }
  if ($1 == update) {
    %damp.socket.download = damp.exe
    %damp.socket.action = update  
  }
  elseif ($1 == skin) {
    %damp.socket.download = skins.exe
    %damp.socket.action = skin
  }
  elseif ($1 == version) {
    %damp.socket.download = version.txt
    %damp.socket.action = version
  }
  else { return }  
  dl
}
alias -l sc.dl return damp.socket.download
alias -l dl { sockclose $sc.dl | prog 0 Connecting... | sockopen $sc.dl www.ileet.net 80 }
alias -l dlf return $f($scriptdirdownload.exe )
on *:sockopen:damp.socket.download: {
  if ($sockerr) { decho Connection error $+([,$sockerr,],.) | prog -c | return }
  if (%damp.socket.download == version.txt) {
    sockwrite -n $sockname GET http://www.ileet.net/~smallmoskito/damp/version.txt
    sockwrite -n $sockname
    prog 0 Connecting...
    return
  }
  %damp.socket.ready = 0
  .timerwebout 1 10 webout
  if ($isfile($dlf)) { .write -c $dlf }
  sockwrite -n $sockname GET http://www.ileet.net/~smallmoskito/damp/ [ $+ [ %damp.socket.download ] ] HTTP/1.0
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname Host: $sock($sockname).ip
  sockwrite -n $sockname
  prog 0 Connecting...
}
alias -l webout {
  prog 0 TIMED OUT
  sockclose $sc.dl
}
alias -l decho echo $color(info) -a [Damp] $$1-
on *:sockread:damp.socket.download: {
  .timerwebout off
  if ($sockerr) { decho Connection error $+([,$sockerr,],.) | return }
  if (%damp.socket.download == version.txt) {
    var %t 
    sockread %t
    while (%t) || ($sockbr) {
      if ($numtok(%t,32) == 1) && (%t > $damp.version(2)) {  
        prog 0 Updates found. Preparing download... 
        sockclose $sockname
        .timer -o 1 1 damp.download update
        return
      }
      sockread %t     
    } 
    prog 100 No Updates.
    sockclose $sc.dl
    return
  }
  if (%damp.socket.ready != 1) {
    var %header | sockread %header
    while ($sockbr) {
      if (Content-length: * iswm %header) {  %damp.socket.length = $gettok(%header,2,32) }
      elseif (* !iswm %header) { %damp.socket.ready = 1 | %damp.socket.offset = $sock($sockname).rcvd | break  }
      sockread %header
    }
  }
  sockread 4096 &d
  while ($sockbr) {
    var %c $int($calc(100 * ($sock($sockname).rcvd - %damp.socket.offset) / %damp.socket.length))
    prog %c $iif((%c == 100),Done,Downloading...) 
    bwrite $dlf -1 -1 &d 
    sockread 4096 &d
  }
  if (%c >= 100) { dl.action }
}
alias dl.action {
  if (%damp.socket.action == update) {
    .run $dlf
    .timer -o 1 1 .remove $dlf
    .timer -o 1 2 decho Updated Damp to version $damp.version(1)
    .timer -o 1 2 decho Please read the update.txt for more information.
    .timer -o 1 2 prog -c
    .timer -o 1 2 .reload -rs $script
    return
  }
  if (%damp.socket.action == skin) {
    var %a $scriptdirskins_ $+ $replace($date,/,_)
    mkdir %a
    .copy $dlf %a $+ \download.exe 
    .run %a $+ \download.exe
    .timer -o 1 2 .remove %a $+ \download.exe
    .timer -o 1 1 .remove $dlf
    .timer -o 1 2 prog -c
    decho Downloaded and installed NEW skins to %a
    decho To get all skins, please visit http://www.ileet.net/~smallmoskito/ for more information.
    return
  }
}

; :: skins command (NOT REVEALED!)
alias -l _set set %damp. [ $+ [ $1 ] ] $2-
alias -l _unset unset %damp. [ $+ [ $1 ] ] 

; DIALOGS

dialog -l damp.skins {
  size -1 -1 150 132
  option dbu
  title "Skins"
  box "All skins",2,5 5 140 87
  list 1,10 15 130 75,hsbar
  text "Skins directory:",3,5 102 40 12
  edit $skindir ,4,44 100 86 12, read
  button "...",5,132 100 10 12, default
  button "&Refresh",7,5 118 28 10, default
  button "&Use selected",8,35 118 38 10, default disable
  button "&Edit selected",9,75 118 38 10, default disable
  button "&Close",6,118 118 28 10, default ok
}

on *:dialog:damp.skins:*:*: {
  var %e $devent , %d $did , %n $dname
  if (!$did(1,$did(1).sel)) { did -b %n 8,9 } | else { did -e %n 8,9 }
  if (%e == sclick) {
    if (!$did(1,$did(1).sel)) { did -b %n 8,9 } | else { did -e %n 8,9 }
    if (%d == 5) { %damp.skindir = $$sdir($skindir,Select a directory,Ok) | listallskin }
    elseif (%d == 7) { listallskin }
    elseif (%d == 8) { if (!$did(1,$did(1).sel)) { return } | damp $did(1,$did(1).sel)) }
    elseif (%d == 9) { if (!$did(1,$did(1).sel)) { return } | run notepad $f($+($skindir,$did(1,$did(1).sel)))) }
  }
  if (%e == dclick) && (%d == 1) { if (!$did(1,$did(1).sel)) { return } | damp $did(1,$did(1).sel)) }
}

alias damp.search if (!$dialog(damp.search)) { dialog -md damp.search damp.search }
dialog -l damp.search {
  size -1 -1 260 145
  option dbu
  title "Damp - Creativity to the max!"

  box "Search for:",3,5 2 80 30
  edit "",2,10 12 58 12, autohs
  button "&Go",23,70 12 12 12, default

  box "Stats:",7,5 34 80 94
  text "Cache file size:",8,10 46 70 12, hide
  text "Total files in playlist:",9,10 46 70 12

  radio "Search playlist",80,15 58 50 10, group checked
  radio "Search harddisc",81,15 70 50 10

  edit "",82,25 80 46 12, read autohs
  button "...",83,72 80 10 12, default

  ; text "Cache location:",25,10 70 50 12
  ; edit "",26, 10 78 70 12, read autohs

  ;  button "&Add directory",10,8 114 46 12, default
  button "&Refresh",4,56 114 26 12, default

  box "Search results:",11,88 2 170 72
  list 12,94 10 160 70, vsbar 

  box "Song info:",13,88 74 170 54

  text "Filename:",104,94 82 75 10
  text "Artist:",105,94 92 75 10
  text "Title",106,94 102 75 10
  text "Length:",107,94 112 75 10
  text "Quality:",108,170 82 85 10
  text "Bitrate:",109,170 92 85 10
  text "Bytes/sec:",200,170 102 85 10

  edit "",14,118 80 50 10, read autohs
  edit "",15,118 90 50 10, read autohs
  edit "",16,118 100 50 10, read autohs
  edit "",17,118 110 50 10, read autohs
  edit "",18,196 80 60 10, read autohs
  edit "",19,196 90 60 10, read autohs
  edit "",20,196 100 60 10, read autohs

  ;combo 21,170 114 70 50, default drop 
  button "&Play song",22,196 114 60 10, default

  text "",98,5 136 175 12

  ;  button "&?",99,248 134 10 10,default
  button "&Close",100,208 134 50 10, default ok
}
alias -l de did -ar damp.search 98 $$1-
on *:dialog:damp.search:*:*: {
  var %e $devent, %d $did , %n $dname
  if (!$did(12).sel) { did -b %n 22 } | else { did -e %n 22 }
  if (%e == close) {
    var %i $window(@damp.search_*,0)
    while (%i) {
      window -c $window(@damp.search_*,%i)
      dec %i
    }
  }
  if (%e == active) { if ($window(@damp)) { cmd.button.min } }
  if (%e == init) { 
    if (!$window(@damp)) { damp | cmd.button.min }
    did -c %n 80
    did -ar %n 82 $musicdir
    .timer -o 1 0 fill_cache 
  }
  if (%e == edit) && (%d == 2) { if (!$did(2).text) { did -b %n 23 } | else { did -e %n 23 } }
  if (%e == sclick) {

    if (%d == 83) { 
      var %v = $$sdir($iif($did(82),$ifmatch,$scriptdir),Select location to search for mp3,Ok) 
      did -ar damp.search 82 %v
      %damp.musicdir = %v
    }
    if (%d == 23) { 
      did -r %n 12 
      if ($did(80).state) { search_cache }
      elseif ( ($did(81).state) && ($isdir($did(82))) ) { search_disc }
    }
    if (%d == 12) { fill_info }
    if (%d == 10) { if (!$window(@damp)) { damp | .timer -om 1 0 cmd.button.min } | cmd.pl.adddir 
      savebuf @damp.cache $f($damp.search_file) 
      cmd.pl.exit | fill_cache | de Added songs to playlist.
    }
    if (%d == 4) { fill_cache | de Refreshed! }
    if (%d == 22) { 
      if (!$window(@damp)) { damp }
      if ($did(80).state) {
        var %f $line(@damp.search_result,$did(12).sel) 
        sline @damp.cache $gettok(%f,1,32) 
      }
      elseif ($did(81).state) { 
        build.playlist c
        iline @damp.cache 1 $gettok($line(@damp.search_result,$did(12).sel),2-,32) 
        sline @damp.cache 1
      }
      cmd.button.play | de Playing song.
      pl.update.list
    }
  }
  if (%e == dclick) && (%d == 12) {
    if (!$window(@damp)) { damp } 
    if ($did(80).state) {
      var %f $line(@damp.search_result,$did(12).sel) 
      sline @damp.cache $gettok(%f,1,32) 
    }
    elseif ($did(81).state) { 
      build.playlist c
      iline @damp.cache 1 $gettok($line(@damp.search_result,$did(12).sel),2-,32) 
      sline @damp.cache 1
    }
    cmd.button.play 
    de Playing song. 
    pl.update.list 
  }
}
alias -l fill_info {
  if (!$window(@damp.search_result)) { return }
  var %n @damp.search_result , %l $did(damp.search,12).sel
  if ($line(%n,%l)) { var %v $shortfn($gettok($ifmatch,2-,32)) } | else { return }
  if (!$isfile(%v)) { return }
  var %d did -o damp.search 
  %d 14 1 $longfn(%v)
  %d 15 1 $sound(%v).artist
  %d 16 1 $sound(%v).title
  %d 17 1 $t($sound(%v).length)
  %d 18 1 $sound(%v).mode
  %d 19 1 $sound(%v).bitrate
  %d 20 1 $int($calc($sound(%v).sample /1000)) khz
}
alias -l fill_cache {
  de Caching files...
  if (!$window(@damp.search_cache)) { window -lh @damp.search_cache }
  else { .clear @damp.search_cache }
  .window -lh @_
  var %f $f($damp.search_file)
  if (!$isfile(%f)) { de You have an empty playlist! Please add in some songs! | return }
  .loadbuf @_ $f($damp.search_file)
  filter -ww @_ @damp.search_cache *.mp3
  .window -c @_
  de Verifying cache file format...
  var %i 15
  while (%i) {
    var %t = $line(@damp.search_cache,%i)
    if (!%t) { dec %i | continue }
    if (%t != $longfn(%t)) { convertlongfn @damp.search_cache | break }
    dec %i
  }
  did -ar damp.search 8 Cache file size: $int($calc($file($damp.search_file).size /1024)) kb.
  did -ar damp.search 9 Total cache files: $filtered files.
  de Done caching files.
}
alias damp.search_file {
  var %n $scriptdirsearch\playlist.dat
  .write -c $f(%n)
  if ($window(@damp.cache)) { .savebuf @damp.cache $f(%n) }
  return %n 
  else { return $scriptdir }
}
alias -l search_cache {
  de Searching files...
  if (!$window(@damp.search_cache)) { fill_cache }
  if (!$line(@damp.search_cache,0)) { return $e(1) }
  if (!$window(@damp.search_result)) { window -lh @damp.search_result }
  else { clear @damp.search_result }
  .filter -wwn @damp.search_cache @damp.search_result [ [ $+(*,$replace($did(2).text,$chr(32),*),*) ] ] 
  var %i $filtered
  while (%i) {
    did -i damp.search 12 1 $nopath($longfn($gettok($line(@damp.search_result,%i),2-,32)))
    dec %i
  }
  did -c damp.search 12 1 
  fill_info
  de Found $filtered $iif(($filtered > 1),files.,file.)
}
alias -l search_disc {
  de Searchin files...
  window -lh @_ 
  var %v $findfile($did(damp.search,82),*.mp3,0,aline @_ $$1-)
  if (!$window(@damp.search_result)) { window -lh @damp.search_result }
  else { clear @damp.search_result }
  .filter -wwn @_ @damp.search_result [ [ $+(*,$replace($did(2).text,$chr(32),*),*) ] ] 
  var %i $filtered
  while (%i) {
    did -i damp.search 12 1 $nopath($longfn($gettok($line(@damp.search_result,%i),2-,32)))
    dec %i
  }
  did -c damp.search 12 1 
  fill_info
  de Found $filtered $iif(($filtered > 1),files.,file.)
  window -c @_
}
alias -l add_directory {
  de Adding cache directory...
  var %d = $$sdir($musicdir,Select directory,Ok)
  var %w @damp.search_add
  if (!$window(%w)) { window -lh %w }
  clear %w
  var %r = $findfile(%d,*.mp3,0,.write $f($damp.search_file) $$1- )
  fill_cache
  de Directory added.
}
alias -l convertlongfn {
  var %w $1 , %f $damp.search_file
  var %i $line(%w,0) , %s %i
  while (%i) {
    prog $int($calc((%s - %i) / %i *100)) Converting cache to longfn...
    rline %w %i $longfn($line(%w,%i))
    dec %i
  }
  savebuf %w $f(%f)
  prog -c 
}

; :: GUI option dialog