Clock! — File Browser

daniel-clock / daniel-clock / clock_challenge.mrc

clock_challenge.mrc — 7.74 KB — Download this file

on *:LOAD:init
on *:START:unset %c!.res %c!.fsize %c!.closing | if (%c!.start) { unset %c!.start | .clock! }
alias -l init %c! = 1 | %c!.cfg = 0 15 0 | %c!.font = 0 Arial | %c!.cl = $rgb(face) 255 128 0 $rgb(shadow) $rgb(shadow) $rgb(text) $rgb(text)

alias -l w return @Clock!
alias clock! {
  if (%c! != 1) { init }
  if ($window($w)) { window -a $w | return } | window $dat $dat¹ $w $cpos $w | font $iif($finfo(1), -b) $w 20 $finfo(2-) | %c!.wh = $window($w).w $window($w).h | update 
  if ($ccfg(1, 3) = 0) && ($show) { titlebar $w - right-click for options | .timerC!tb 1 2 titlebar $w } | .timerC! -o 0 1 update | .timerC!chk -om 0 50 chk
}
alias -l dat var %r = return -pkf | if ($ccfg(1, 1)) { if ($ccfg(1, 2)) { %r $+ do } | %r $+ d } | %r
alias -l dat¹ if ($ccfg(1, 3)) { return +bL } | return +tsL
alias -l cpos {
  if (%c!.pos) { return $ifmatch }
  var %r = 0.5, %wid = $iif($ccfg(1, 1), -1, -3), %w = $int($calc($window(%wid).w * %r))
  if ($window(%wid).w < %w) { %w = $ifmatch } | if ($window(%wid).h < %w) { %w = $ifmatch }
  return $int($calc(($window(%wid).w - %w) / 2)) $int($calc(($window(%wid).h - %w) / 2)) %w %w
}
alias -l ccfg return $isbit($gettok(%c!.cfg, $1, 32), $2)
alias -l ccl return $gettok(%c!.cl, $1, 32)
on *:KEYDOWN:$($w):13:c!config
on *:KEYDOWN:$($w):65:c!config

on *:CLOSE:$($w):.close | %c!.start = 1 | .timer 1 0 unset % $+ c!.start
on *:KEYDOWN:$($w):27:%c!.closing = 1
on *:KEYUP:$($w):27:if (%c!.closing) { close }
alias -l close {
  %c!.pos = $window($w).x $window($w).y $window($w).dw $window($w).dh | unset %c!.wh %c!.d-? %c!.fsize %c!.closing | .timerC!* off | if ($show) { window -c $w }
}

menu @Clock! {
  sclick:%c!.d-x = $mouse.x | %c!.d-y = $mouse.y
  dclick:cs 1 3
  uclick:unset %c!.d-?
  mouse:if ($mouse.key & 1) && (%c!.d-x != $null) { window $w $calc($window($w).x - (%c!.d-x - $mouse.x)) $calc($window($w).y - (%c!.d-y - $mouse.y)) }
  drop:unset %c!.d-?
  leave:unset %c!.d-?
  &Mode
  .$nmnu(1, 4) &Analog:su 1 4
  .$mnu(1, 4) &Digital:su 1 4
  -
  $mnu(1, 3) &Compact:cs 1 3
  $mnu(1, 1) &Desktop:{
    close | var %x = $gettok(%c!.pos, 1, 32), %y = $gettok(%c!.pos, 2, 32), %cmd = inc | if ($ccfg(1, 1)) { %cmd = dec }
    %cmd %x $window(-3).x | %cmd %y $window(-3).y | switch 1 1 | %c!.pos = %x %y $gettok(%c!.pos, 3-4, 32) | .clock!
  }
  $mnu.ontop:cs 1 2
  -
  $mnu.an(&Pointers)
  .$mnu(2, 1) &Hours:su 2 1
  .$mnu(2, 2) &Minutes:su 2 2
  .$mnu(2, 3) &Seconds:su 2 3
  $mnu.an(&Marks)
  .$mnu(2, 4) Every &hour:su 2 4
  .$mnu(2, 5) Every &minute:su 2 5
  $mnu.dg(&Font...):font | %c!.font = $iif($window($w).fontbold, 1, 0) $window($w).font | unset %c!.fsize | update
  $mnu.dg(3, 1, &Show date):unset %c!.fsize | su 3 1
  C&olors
  .&Background...:gcl 1
  .$mnu.an(&Pointers)
  ..&Hours...:gcl 2
  ..&Minutes...:gcl 3
  ..&Seconds...:gcl 4
  .$mnu.an(&Marks)
  ..&Hours...:gcl 5
  ..&Minutes...:gcl 6
  .$mnu.dg(&Time...):gcl 7
  .$mnu.dg(&Date...):gcl 8
}
alias -l cs close | switch $1-2 | .clock!
alias -l su switch $1-2 | update
alias -l mnu if ($ccfg($1, $2)) { return ø } | return o
alias -l nmnu if ($ccfg($1, $2)) { return o } | return ø
alias -l mnu.ontop if ($ccfg(1, 1)) { return $mnu(1, 2) On top }
alias -l mnu.an if ($ccfg(1, 4) = 0) { if ($0 = 1) { return $1 } | return $mnu($1, $2) $3 }
alias -l mnu.dg if ($ccfg(1, 4)) { if ($0 = 1) { return $1 } | return $mnu($1, $2) $3 }
alias -l gcl var %cl = c!color. $+ $1, %cl = $dialog(%cl, c!color, -3) | if (%cl != $null) { %c!.cl = $puttok(%c!.cl, %cl, $1, 32) | update }

dialog c!color {
  title "Change color"
  size -1 -1 112 37
  option dbu

  text "Color value:", 1, 2 2 28 7
  edit "", 2, 40 0 51 11, right
  text "Type color values in hex format, like in HTML.", 3, 2 12 108 7
  text "", 4, 0 0 0 0, hide, result

  box "", 497, -1 18 114 20
  button "OK", 499, 10 25 50 10, default, ok
  button "Cancel", 500, 60 25 50 10, cancel
}
on *:DIALOG:c!color.*:init:0:{
  var %rgb = $rgb($ccl($gettok($dname, 2, 46))), %txt = $hx(%rgb, 1) $+ $hx(%rgb, 2) $+ $hx(%rgb, 3)
  did -o $dname 2 1 %txt | did -c $dname 2 1 1 $calc($len(%txt) + 1)
}
on *:DIALOG:c!color.*:edit:2:{
  var %tok = $did($did) | if (#* iswm %tok) { %tok = $mid(%tok, 2) } | else { %tok = $replace(%tok, $chr(44), $chr(32)) } | tokenize 32 %tok | var %ok = 0
  if ($0 = 1) && ($remove($1, a, b, c, d, e, f, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) = $null) && (. !isin $1) && ($len($1) = 6) { %ok = 1 }
  elseif ($0 = 3) && ($1 isnum 0-255) && ($2 isnum 0-255) && ($3 isnum 0-255) && (. !isin $1-) { %ok = 1 }
  if (%ok) && ($did(499).enabled = $false) { did -e $dname 499 } | elseif (%ok = 0) && ($did(499).enabled) { did -b $dname 499 }
}
on *:DIALOG:c!color.*:sclick:499:{
  var %tok = $did(2) | if (#* iswm %tok) { %tok = $mid(%tok, 2) } | else { %tok = $replace(%tok, $chr(44), $chr(32)) } | tokenize 32 %tok | var %ret
  if ($0 = 1) { %ret = $rgb($dc($1, 1), $dc($1, 3), $dc($1, 5)) } | elseif ($0 = 3) { %ret = $rgb($1, $2, $3) } | did -o $dname 4 1 %ret
}
alias -l hx return $base($gettok($1, $2, 44), 10, 16, 2)
alias -l dc return $base($mid($1, $2, 2), 16, 10)

alias -l update {
  if (%c!.res) { return } | clear -n $w | drawrect -nfr $w $ccl(1) 0 0 0 $window($w).dw $window($w).dh | if ($ccfg(1, 4)) { upd.d } | else { upd.a } | drawdot $w
}
alias -l upd.a {
  var %r = $window($w).dw, %x = %r / 2, %y = $window($w).dh / 2 | if ($window($w).dh < %r) { %r = $ifmatch } | %r = %r / 2
  if ($ccfg(2, 5)) { domarks $ccl(6) %x %y $calc(0.9 * %r) $calc(0.95 * %r) 60 }
  if ($ccfg(2, 4)) { domarks $ccl(5) %x %y $calc(0.8 * %r) $calc(0.95 * %r) 12 }
  if ($ccfg(2, 3)) { pntr $calc($time(s) / 60) $calc(0.95 * %r) $ccl(4) $calc(%r / 48) %x %y }
  if ($ccfg(2, 2)) { pntr $calc(($time(n) + $time(s) / 60) / 60) $calc(0.9 * %r) $ccl(3) $calc(%r / 24) %x %y }
  if ($ccfg(2, 1)) { pntr $calc(($time(h) + ($time(n) + $time(s) / 60) / 60) / 12) $calc(0.75 * %r) $ccl(2) $calc(%r / 16) %x %y }
}
alias -l pntr var %a = $calc(($1 - 0.25) * 6.28319) | drawline -nr $w $3-6 $calc($5 + $cos(%a) * $2) $calc($6 + $sin(%a) * $2)
alias -l domarks {
  var %i = 1, %t = $6, %a
  while (%i <= %t) {
    %a = $calc(%i / %t * 6.28319)
    drawline -nr $w $1 1 $int($calc($2 + $cos(%a) * $4)) $int($calc($3 + $sin(%a) * $4)) $calc($2 + $cos(%a) * $5) $calc($3 + $sin(%a) * $5) | inc %i
  }
}

alias -l chk {
  var %wh = $window($w).w $window($w).h
  if (%wh != %c!.wh) {
    %c!.wh = %wh | %c!.res = 1 | drawrect -rf $w $rgb(face) 0 0 0 $window($w).dw $window($w).dh
    .timerC!res -om 1 250 unset % $+ c!.res % $+ c!.fsize $chr(124) update
  }
}

alias -l switch var %t = $gettok(%c!.cfg, $1, 32) | if ($isbit(%t, $2)) { %t = $bitoff(%t, $2) } | else { %t = $biton(%t, $2) } | %c!.cfg = $puttok(%c!.cfg, %t, $1, 32)

alias -l upd.d {
  var %f = $finfo(2-), %fb = $finfo(1), %fs = %c!.fsize, %txt = $time(hh:nn:ss tt), %txt¹ = $time(ddd dd/mm/yy) 
  if (%fs = $null) { %fs = $newfsize(%txt, %f, %fb) | %c!.fsize = %fs }
  var %w = $width(%txt, %f, %fs, %fb, 0), %h = $calc(%fs * (1 + $ccfg(3, 1) / 2))
  var %x = $calc(($window($w).dw - %w) / 2), %y = $calc(($window($w).dh - %h) / 2), %p = -rn | if (%fb) { %p = %p $+ o }
  drawtext %p $w $ccl(7) " $+ %f $+ " %fs %x %y %txt
  if ($ccfg(3, 1)) {
    inc %y %fs | %fs = %fs / 2 | %w = $width(%txt¹, %f, %fs, %fb, 0) | %x = $calc(($window($w).dw - %w) / 2)
    drawtext %p $w $ccl(8) " $+ %f $+ " %fs %x %y %txt¹
  }
}
alias -l newfsize {
  var %s = 5, %w, %h, %ls, %lw, %lh, %dw = $window($w).dw, %dh = $window($w).dh, %t = %dh, %m = $calc(1 + $ccfg(3, 1) / 2)
  while (%s <= %t) {
    %w = $width($1, $2, %s, $3, 0) | %h = $calc(%s * %m) | if ((%w >= %dw) || (%h >= %dh)) && ((%lw < %dw) || (%lh < %dh)) { return %ls }
    %lw = %w | %lh = %h | %ls = %s | inc %s
  }
  return %t
}

alias -l finfo return $gettok(%c!.font, $1, 32)