All pastes #192485 Raw Edit

talim

public text v1 · immutable
·published 2006-10-05 23:54 UTC
rendered paste body
alias init {

  ;Example alias, limited to one list per @window at the moment.

  window -ak0hdBpfo +d @test -1 -1 300 300 

  $listbox(@test, 100 100 150 100).coords
  $listbox(@test, Just testing.).header
  $listbox(@test, 50 50).columns
  $listbox(@test, verdana/10).font
  $listbox(@test, test).hash

  listbox @test

}

alias listbox {
  var %t = listbox. $+ $1
  if ($isid) {
    hadd -m %t $prop $2-
  }
  else {
    var %win = $1
    tokenize 32 $hget(listbox. $+ %win, coords)
    drawrect -rfn %win $rgb(223,220,214) 1 $1-
    draw.detail %win $false $1-
    draw.header %win $calc($1 + 3) $calc($2 + 3) $calc($3 - 6)
    draw.buffer %win
    drawdot %win
  }
}

alias draw.detail {
  var %a, %b
  if ($2) {
    %a = $rgb(255,255,255) | %b = $rgb(144,144,144)
  }
  else {
    %b = $rgb(255,255,255) | %a = $rgb(144,144,144)
  }
  drawline -rn $1 %a 2 $3 $calc($4 + $6) $3 $4 $calc($3 + $5) $4
  drawline -rn $1 %b 2 $3 $calc($4 + $6) $calc($3 + $5) $calc($4 + $6) $calc($3 + $5) $4
}

alias draw.header {
  var %win = $1, %font = $hget(listbox. $+ %win, font), %text = $hget(listbox. $+ %win, header), %h = $font.height(%font)
  draw.detail %win $true $2- %h
  set.list.dims %win $2- %h
  tokenize 32 $calc($2 + 3) $3 $calc($4 - 6) %h
  %font = $replace(%font, /, $chr(32))
  drawtext -rnc %win $rgb(144,144,144) %font $1 $2 $calc($3 - 1) $calc($4 - 1) %text
  drawtext -rnc %win $rgb(160,160,160) %font $calc($1 + 1) $calc($2 + 1) $3 $4 %text
}

alias set.list.dims {
  var %win = $1
  tokenize 32 $2 $calc($3 + $5) $calc($4 - 15) $calc(($gettok($hget(listbox. $+ $1, coords), 4, 32) - $5) - 3)
  $listbox(%win, $1-).listdims
  $listbox(%win, $calc($1 + $3) $calc($2 + 2) 15 $calc($4 - 4)).scrolldims
}

alias draw.buffer {
  if ($2) {
    $listbox($1, $ifmatch).position
  }
  var %win = $1, %p = $iif($hget(listbox. $+ %win, position), $ifmatch, 1), %step = $font.height($hget(listbox. $+ %win, font)), %hash = $hget(listbox. $+ %win, hash), %i = 0, %z = 1
  tokenize 32 $hget(listbox. $+ %win, listdims)
  %font = $replace(%font, /, $chr(32))
  while (%i < $calc($4 - %step)) {
    if ($hget(%hash, ITEM $+ $calc(%p + %i))) {
      draw.row %win $1 $calc($2 + %i) $3 $4 $ifmatch
      inc %i %step
      inc %z
    }
    else {
      break
    }
  }
  $scrollbar(%win, %z).listlines
  var %a = 1, %b = $1, %c
  while ($gettok($hget(listbox. $+ %win, columns), %a, 32)) {
    %c = $ifmatch
    draw.line %win $calc(%b + %c) $2 $calc(%b + %c) $calc($2 + $4)
    inc %b %c
    inc %a
  }
  draw.scroll %win 
  draw.select %win
}

alias draw.row {
  var %win = $1, %font $replace($hget(listbox. $+ $1, font), /, $chr(32)), %columns = $hget(listbox. $+ $1, columns), %i = 1, %x, %z, %t
  tokenize 32 $2-
  %x = $1
  while ($gettok(%columns, %i, 32)) {
    %z = $ifmatch 
    if ($gettok($5-, %i, 9)) {
      drawtext -rnc %win $rgb(144,144,144) %font %x $2 %z $4 $ifmatch
      drawtext -rnc %win $rgb(160,160,160) %font $calc(%x + 1) $calc($2 + 1) $calc(%z - 1) $calc($4 - 1) $ifmatch
    }
    inc %x %z
    inc %i
  }
}

alias draw.line {
  var %win = $1
  tokenize 32 $2-
  drawline -rn %win $rgb(255,255,255) 1 $1-
  drawline -rn %win $rgb(144,144,144) 1 $calc($1 + 1) $2 $calc($3 + 1) $4
}

alias draw.scroll {
  var %p = $hget(listbox. $+ $1, position), %win = $1
  tokenize 32 $hget(listbox. $+ %win, scrolldims) $hget($hget(listbox. $+ %win, hash), ITEM0) $hget(listbox. $+ %win, listlines)
  var %s = $iif($5 <= $6, 1, $iif($calc($6 / $5) < 0.07, 0.07, $v1))), %x = $1, %y = $int($calc(((%p / 100 * $4) * (1 - %s)) + $2)), %w = $calc($3 - 1), %h = $ceil($calc($4 * %s - 1))
  drawrect -rnf %win $rgb(223,220,214) 1 $1 $2 $3 $4
  draw.detail %win $true %x %y %w %h
}

alias draw.select {
  var %win = $1
  if ($2) {
    var %s = $ifmatch
    if ($hget(listbox. $+ %win, selected_line)) {
      tokenize 32 $hget(listbox. $+ %win, listdims)

      drawreplace -nr %win $rgb(219,225,232) $rgb(243,240,234) 14 %s 139 13
      drawrect -rn %win $rgb(243,240,234) 1 14 %s 139 13

    }

    $listbox(%win, %s).selected_line

  }

  drawreplace -nr @test1 $rgb(243,240,234) $rgb(219,225,232) 14 $calc((%z * 13) + 30) 139 13

  drawrect -nr @test1 $rgb(157,167,177) 1 14 $calc((%z * 13) + 30) 139 13

}

alias font.height {
  tokenize 47 $1-
  return $calc($height(T, $1, $2) + 4)
}

menu * {
  sclick: {
    if ($hget(listbox. $+ $active)) {
      var %t = $ifmatch
      tokenize 32 $hget(%t, listdims) 
      if ($inrect($mouse.x, $mouse.y, $1, $2, $3, $4)) {
        draw.select $active $calc(($mouse.x / $4) * $hget(%t, listlines))
      }
    }
  }
}