Damp — File Browser

Dampv1.0 / Dampv1.0 / damp.mrc

damp.mrc — 38.93 KB — Download this file

;Damp
;mIRC version check
on *:load: {
  var %e echo $color(info) -a [Damp]
  if ($version < 6.03) { 
    %e-
    %e You have mIRC version $version
    %e Damp is not gurantee to work!
    %e -
  }
  %e Usage:
  %e /damp [skin]
  %e If you specify a skin file when running Damp, you will not be prompted to select a skin file.
}

menu menubar,status,channel,query {
  &Damp
  .&Select skin: damp
  .-
  . $submenu($listskin($1))
}
alias -l listskin {
  if (!$1) { return }
  if ($1 == begin) { window -hl @listskin | var %v $findfile($scriptdir,*.dsf,0,aline @listskin $$1-) }
  if ($1 <= $line(@listskin,0)) { return & $+ $nopath($line(@listskin,$1)) : damp $nopath($shortfn($line(@listskin,$1))) }
  if ($1 == end) { window -c @listskin }
}
alias damp {
  var %s = $scriptdir $+ $1-
  if (!%s) || (!$isfile(%s)) {
    var %s $$sfile($scriptdir,Select a skin,Ok)
  }
  if ($gettok(%s,-1,46) != dsf) { return }
  .window -hl @damp.cache.cmd
  .loadbuf @damp.cache.cmd $f(%s)
  if ($hget(damp.cache)) { 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
  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)
  dbox1 0 0 $rd(w) $rd(h)
  cmd.build.pl -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) { .clear @damp.cache | build.playlist + %damp.musiclist | cmd.build.pl -s }
  dd | ddl
}

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 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.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 (!%cood) { return }
  tokenize 44 %cood
  drawrect -frn %win $c2 1 $1-
  dbox2 %win $1-
  var  %x $calc(%percent / 100 * $3 * (1- $5) + ($1 +1))   , %h $calc($4 /5 *3 +1) , %w $calc($5 * $3 +1) , %y $calc(%h / 3 + $2)
  if ($3 >= $4) {
    ;draw the trackbar - horizontal
    drawrect -frn %win $c1 1 %x %y %w %h
    dbox1 %win %x %y %w %h
  }
  elseif ($4 > $3) {
    var %w $calc($3 / 5 *3) ,%y $calc($2 + ($4 - %percent /100 * $4 * (1 - $5)))  , %h $calc($4 * $5) , %x $calc($1 + %w /3)
    if (%win = $wl) { %y = $calc($2 + %percent / 100 * $4) }
    drawrect -frn %win $c1 1 %x %y %w %h
    dbox1 %win %x %y %w %h
  }
}

;draw blank boxes
alias -l dbox1 {
  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  
}

alias -l dbox2 {
  var %w = $iif(($1 == $wl),$wl,$w) | if ($1 isin $w $wl) { tokenize 32 $2-$5 }
  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  
}

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

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

alias -l build.button.min {
  var %cood $rd(button.min) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $w $c3 $button.text(tahoma,$5, $1,$2,$3,$4,_) 
  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) +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 
  %h = $height(T,$5,$6) 
  if (%h > $4) { %h = $calc($4 -8) } 
  if ($rd(scroll)) { drawtext -rcn $w $c3 $5 $6  $1 $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))
  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) > $gettok($box.song,3,44)) { v scroll 1 | v tickname $rd(tickname) ~ }
  else { v scroll 0 | text.song $rd(tickname) | draw.text.song }
  pl.update.list
  .timerticker -m 0 200 damp.ticker
}

alias -l damp.ticker {
  if (!$rd(trackbar.progress.ignore)) {
    trackbar.progress.percent $int($calc($insong.pos / $rd(ticklength) * 100))
    update.trackbar.progress trackbar.progress
  }
  text.time $t($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 -l 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($line(@damp.cache,$sline(@damp.cache,1).ln))
  if (!$isfile(%f)) { return $cmd.button.next }
  if ($window(@damp.list)) { sline @damp.list $sline(@damp.cache,1).ln }
  .splay stop
  .splay %f
  .damp.tick %f
}
alias -l cmd.button.stop {
  .splay stop
  text.time 0:00
  text.song Damp - Creativity to the max!
  v tickname $replace($text.song ~ ,$chr(32),$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 $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 }
  }
  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.list)) { return }
  var %s $sline(@damp.list,1).ln 
  dec %s
  if (!%s) { %s = $line(@damp.cache,0) }
  sline @damp.cache %s
  if (!$line(@damp.cache,%s)) {
    if ($repeat) { sline @damp.cache $line(@damp.cache,0) }
    else { return }
  }
  if ($random) { 
    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 musicdir return $iif(%damp.musicdir,$ifmatch,$scriptdir)

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 }

;mouse
menu @damp {
  mouse:return $mo($mouse.x,$mouse.y)
  sclick:return $sc($mouse.x,$mouse.y)
  uclick: return $uc($mouse.x,$mouse.y)
}

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)) / $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)) / $iif(($3 >= $4),$3,$4) * 100) 
      if ($3 < $4) { %percent = $calc(100 - %percent) }
      trackbar.volume.percent %percent
      update.trackbar.progress trackbar.volume
      return
    }
  }
  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(($ielseif(($3 >= $4), %x - $1,%y - $2)) / $ielseif(($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($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 * $trackbar.volume.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 -o $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 }
}
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).status == 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,Damp Playlist) 
  playlist.exit $calc($1 + $3 -14) $+ , $+ $calc($2 + $4 / 5) $+ , $+ 12 $+ , $+ 12
  build.pl.exit
}
alias -l build.pl.exit {
  var %cood $rd(pl.exit) | if (!%cood) { return }
  tokenize 44 %cood | dbox1 $wl $1-4 | drawtext -rcn $wl $c3 $button.text(marlett,10, $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
  playlist.scroller $calc($1 + $3 -12) $+  , $+ $calc($2 + 14) $+  , $+ 12 $+  , $+ $calc($4 - 28) , $+ 0.05
  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
  dbox1 $wl $1-4 | drawtext -rcn $wl $c3 $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
  dbox1 $wl $1-4 | drawtext -rcn $wl $c3 $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 $c3 $button.text($5,$6,$1,$2,$3,$4,+ song) | dbox1 $wl $1-4
}
alias -l build.pl.delsong { 
  var %cood $rd(pl.delsong) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c3 $button.text($5,$6,$1,$2,$3,$4,- song) | dbox1 $wl $1-4
}
alias -l build.pl.adddir { 
  var %cood $rd(pl.adddir) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c3 $button.text($5,$6,$1,$2,$3,$4,+ dir) | dbox1 $wl $1-4
}
alias -l build.pl.addlist {
  var %cood $rd(pl.addlist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c3 $button.text($5,$6,$1,$2,$3,$4,+ list) | dbox1 $wl $1-4
}
alias -l build.pl.dellist { 
  var %cood $rd(pl.dellist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c3 $button.text($5,$6,$1,$2,$3,$4,- list) | dbox1 $wl $1-4
}
alias -l build.pl.savelist { 
  var %cood $rd(pl.savelist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c3 $button.text($5,$6,$1,$2,$3,$4,Save) | dbox1 $wl $1-4
}
alias -l build.pl.deflist {
  var %cood $rd(pl.deflist) | if (!%cood) { return } | tokenize 44 %cood 
  drawtext -rcn $wl $c3 $button.text($5,$6,$1,$2,$3,$4,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 - | 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 | %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)
}

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 | dbox2 $wl $p($rd(pl.scroller)) } 
    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.title)) ] ] )) { 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 | 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) }
    sline @damp.cache %l
    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) }
    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
}
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
  ;if ($calc($line(@damp.cache,0) - %s ) < %l) && ($line(@damp.cache,0) >= %l) { %s = $calc($line(@damp.cache,0) - %l +1) | v scrollline $line(@damp.cache,0) }
  while (%i <= %l) {
    var %v = $nopath($longfn($line(@damp.cache,%s)))
    if (!%v) { inc %i | continue }
    if ($width(%v,$5,$6) >= %w) { %v = $button.text($5,$6,$1,%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  }
    drawtext -rcn $wl $c3 %v
    inc %i
    inc %s
    inc %y %h
  }
  var %p $int($calc($rd(scrollline) / $line(@damp.cache,0) *100))
  v pl.scroll.percent $iif((%p > 95),95,%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 $shortfn($2-) } 
    else { .loadbuf @damp.cache $f($2-)  }
    if ($window(@damp.list)) { ref.playlist }
  }
  if ($1 == -) {
    if ($2 == *) { .clear @damp.cache | return }
    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
    }
  }
}

alias -l ref.playlist {
  if ($rd(pl.list)) && ($window($wl)) { v scrollline $sline(@damp.cache,1).ln | 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 {
  savebuf @damp.cache $$sfile($scriptdir,Select a file,Save)
}

menu @damp.list {
  dclick:sline @damp.cache $sline(@damp.list,1).ln | cmd.button.play 

  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 | %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: build.playlist -
}
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 $rd(scrollline) | pl.update.list }
}