;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; devdev's toobar-based lagbar (enhanced for mIRC-6.x)
; http://www.ScriptaZ.com/
; dev_dev@excite.com
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This bar shows your lag (6.x will show it on the active server) and
; has many new functions from user-suggestions. Background color and lag color can be 
; changed along with the delay of the update and the size of the bar. (ie: max
; lag shown will be 10s) The size will turn red when you max your lag.
; ie: max=5 but you get a 5.1 lag, it will turn red (along with the text).
; Just a fun feature I suppose... in mIRC-6.x, if you're activly on a disconnected
; server, it will show 'offline' in the bar. I figured this kind of multiserver support
; might be necessary for some. You can also use the alias /saylag to message your current
; lag time to the channel, I've incorporated this into the drop down menu on the lagbar
; also, but it only messages to the $active channel.
; 
; I'd like to give credit to dochan for his wonderful tbwin.dll, which allows me to
; attach the lagbar right on the toolbar. Also, thanks to anyone else who has made
; a lagbar, or who has given me suggestions for an improved version. Their ideas
; and features are what drove me to compile everything into one enhanced lagbar.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Modifié et traduis (pour l'utilisateur) par Nisroch.
;
; Ajout de la gestion multi-serveur
; Traduction du menu popup

; edit this line so you can load the dll correctly (location of it)
alias _tbwin return $shortfn($scriptdir) $+ tbwin.dll

; edit this line to your prefrences (it is called when you 'Say lag to #active'
; remember that %L is your lag time
; ie: alias saylag say 4.::14[Lag15: %L $+ 14]4::.
alias saylag { describe $active Lag: %L sur $server }

;;;;;;;;;;;;;;;;;;;;;;;;;
; Lagbar Aliases
;;;;;;;;;;;;;;;;;;;;;;;;;
alias f6 { if (%lagbar != on || @lag == $null) {
    lag
    set %lagbar on
    echo -ag  $+ %col1 $+ >>> $+ %col3 LagBar activée, click droit dessus pour les options.
    if ($dialog(speedconf).title != $null) { did -c speedconf 9 }
  }
  elseif (%lagbar == on || @lag != $null) {
    lagstop
    unset %lagbar %lag.serv %c.time %L %m
    echo -ag  $+ %col1 $+ >>> $+ %col3 LagBar désactivée.
    if ($dialog(speedconf).title != $null) { did -u speedconf 9 }
  }
}

alias tbwin dll $_tbwin $1-
alias lag {
  set %lag.serv $server
  ; Open our window..
  window -Bph +d @lag 720 4 135 20
  drawtext -n @lag %lag.fcolor Tahoma 7 2 1 lag
  ; Attach it...
  tbwin Attach @lag
  tbwin Select @lag
  ; Set the timer to update it.
  lagupdate
  lagserver
  ; Finally, show it.
  window -Ba @lag
}
alias lagserver .timer[lagupdate] 0 %lag.delay lagupdate
alias lagupdate {
  if ($server) {
    if (%lag.bgcolor == $null) {
      echo -a  $+ %col1 !!!  $+ %col2 Couleur de fond de la LagBar invalide, entrez bien au format RGB: 255,0,0  $+ %col1 !!!
      set %lag.bgcolor 0
    }
    set %c.time $ticks
    .raw ping %c.time
    set %L $calc(%m) $+ s
    drawrect -nrf @lag %lag.bgcolor 1 0 0 500 500
    ; Draw the lag bar with its black outline.
    if (%m > %lag.size) {
      drawtext @lag %lag.fcolor Tahoma -7 3 6 lag:
      drawtext -n @lag 4 Tahoma -7 105 6 %l $+ !
      drawrect -n @lag %lag.fcolor 1 22 7 75 10
      drawrect -nf @lag 4 1 24 9 71 6
    }
    else {
      drawtext @lag %lag.fcolor Tahoma -7 3 6 lag:
      drawtext -n @lag %lag.fcolor Tahoma -7 105 6 %l
      drawrect -n @lag %lag.fcolor 1 22 7 75 10
      drawrect -nf @lag %lag.color 1 24 9 $int($calc((75 / %lag.size) * %m)) 6
    }
    ; Update everything.
    drawdot @lag
  }
  else lagoffline
}
alias lagsize window @lag $calc($1 - 200) $int($calc(($2 - 20) / 2)) 200 20
alias -l lagmouse {
  var %x = $dll(tbwin.dll, GetMouse, NOT_USED)
  if ($prop == x) return $gettok(%x, 1, 32)
  elseif ($prop == y) return $gettok(%x, 2, 32)
  elseif ($prop == key) return $gettok(%x, 3, 32)
  else return $null
}
alias lagstop {
  unset %lag.serv
  dll -u $_tbwin
  .timer[lagupdate] off
  if ($window(@lag)) window -c $ifmatch
}
alias lagoffline {
  .timer[lagupdate] off
  window -Bph +d @lag 720 4 135 20
  drawtext -n @lag %lag.fcolor Tahoma 7 2 1 lag
  ; Attach it...
  tbwin Attach @lag
  tbwin Select @lag
  drawrect -nrf @lag %lag.bgcolor 1 0 0 500 500
  drawtext @lag 1 Tahoma -7 3 6 lag:
  drawtext -n @lag 1 Tahoma -7 105 6 offline
  drawrect -n @lag 1 1 22 7 75 10
  drawdot @lag
  window -Ba @lag
}
;;;;;;;;;;;;;;;;;;;;;;;;
; Lagbar Menu
;;;;;;;;;;;;;;;;;;;;;;;;
menu @lag {
  &Couleur de la barre
  .&Rouge:{ set %lag.color 4 | lagupdate }
  .&Bleu:{ set %lag.color 2 | lagupdate }
  .&Vert:{ set %lag.color 3 | lagupdate }
  .&Noir:{ set %lag.color 1 | lagupdate }
  .&Autre:{ set %lag.color $$?="Quelle couleur ? (0 à 15, /coul pour avoir le code couleur)" | lagupdate }
  &Couleur de fond
  .&Face:{ set %lag.bgcolor $rgb(face) | lagupdate }
  .&Rouge:{ set %lag.bgcolor $rgb(255,0,0) | lagupdate }
  .&Bleu:{ set %lag.bgcolor $rgb(0,0,255) | lagupdate }
  .&Vert:{ set %lag.bgcolor $rgb(0,255,0) | lagupdate }
  .&Gris:{ set %lag.bgcolor $rgb(66,66,66) | lagupdate }
  .&Autre:{ set %lag.bgcolor $rgb( $+ [ $$?="Couleur de fond en rgb (ex: 255,0,0)" ] $+) | lagupdate }
  &Couleur du texte
  .&Rouge:{ set %lag.fcolor 4 | lagupdate }
  .&Bleu:{ set %lag.fcolor 2 | lagupdate }
  .&Vert:{ set %lag.fcolor 3 | lagupdate }
  .&Noir:{ set %lag.fcolor 1 | lagupdate }
  .&Autre:{ set %lag.fcolor $$?="Couleur texte et outline (0 à 15, /coul pour avoir le code couleur)" | lagupdate }
  &Taille de la LagBar ( $+ %lag.size $+ s $+ )
  .&Changer:{ set %lag.size $$?="Taille de la barre (max secondes)" }
  &Vitesse de mise à jour ( $+ %lag.delay $+ s $+ )
  .&Changer:{ set %lag.delay $$?="Quel délai ? (secondes)" | .timer[lagupdate] off | lagserver }
  -
  $iif($active ischan,&Dire lag sur $active):{ saylag }
  -
  &Fermer:{ f6 }
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Lagbar Events
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
on ^*:pong:{ if (%lagbar == on) { set %m $calc($calc($ticks - %c.time) / 1000) | halt } }
on *:close:@lag:.timer[lagupdate] off
on *:connect:{ if (%lagbar == on) lag }
on *:disconnect:{ if (%lagbar == on) lagoffline }
on *:start:{ if (%lagbar == on) lagoffline }
on *:active:*:{
  if (%lagbar != on) { goto fin }
  if (%lagbar == on && %lag.serv != $server) { lag } 
  if ($server == $null) { lagoffline }
  :fin
}
