aIRC — File Browser

aIRCv0.8.69 / aIRCv0.8.69 / script / themes.mrc

themes.mrc — 34.25 KB — Download this file

;/**********************************************************************
; *
; * aIRC
; * Theme System
; *
; **********************************************************************/

alias _start.themes {
  _hmake _airctheme 100
  if ($_isfile($_usrdir(theme.dat)) != $null) { hload _airctheme " $+ $ifmatch $+ " }
  $iif($_getsetup(script,_texthemes),.enable,.disable) #_airctexthemes
}
alias _themes.savehash { __hashsavebuf _airctheme theme.dat }

alias theme {
  if ($1- != $null) {
    tokenize 32 $nopath($1-)
    if ($_isint($1-)) {
      if ($findfile($mircdirthemes\,*.theme,$1) != $null) { loadtheme $ifmatch }
      else { error.a No theme: $1 }
    }
    else {
      if (* isin $1-) || (? isin $1-)  {
        var %win = @__theme,%tmp = $1- $+ *,%i = 1
        if (*.theme !iswm %tmp) { %tmp = %tmp $+ .theme }
        while (** isin %tmp) { %tmp = $replace(%tmp,**,*) }
        _openwins %win
        _dummy $findfile($mircdirthemes\,%tmp,0,%win)
        if ($line(%win,1) != $null) { loadtheme $ifmatch }
        else { error.a No theme matching ' $+ $1- $+ ' }
        close -@ %win
      }
      else {
        if (*.theme iswm $1-) {
          if ($findfile($mircdirthemes\,$1-,1) != $null) { loadtheme $ifmatch }
          else { error.a No theme: ' $+ $1- $+ ' }
        }
        else { error.a Invalid theme: ' $+ $1- $+ ' }
      }
    }
  }
  else {
    var %win = @__theme,%i = 1,%n,%echo = echo2 $colour(info) -ait
    _openwins %win
    _dummy $findfile($mircdirthemes\,*.theme,0,aline %win $nopath($1-))
    if ($line(%win,0)) {
      %n = $ifmatch
      %echo $chr(160)
      %echo $^*** Found $_nsingoplur(%n,theme,themes) $+ :
      while (%i <= %n) {
        %echo $^*** %i $nopath($line(%win,%i))
        inc %i
      }
      %echo $^*** Type '/theme number' or '/theme file'
      %echo $^*** Use '/theme *wildcard*' to load the first matching theme
    }
    else { error.a No themes found! }
    close -@ %win
  }
}
alias untheme { unloadtheme }
alias temas { themes $1- }
alias themes { _dialog -m themes themes }
alias tscheme {
  if ($istok(on off,$1,32)) {
    _setsetup script _texthemes $iif($1 == on,1,0)
    _start.themes
  }
  if ($show) {
    if ($_getsetup(script,_texthemes)) { info.a Text Schemes $^b(ON) }
    else { info.a Text Schemes $^b(OFF) - Using mIRC defaults }
  }
}
alias _themesound {
  if (* isin $2-) || (? isin $2-) {
    var %t = $iif(: isin $nofile($2-),$nofile($2-),$mircdir [ $+ [ $nofile($2-) ] ] )
    %t = $_fixdir(%t)
    if ($findfile(%t,$nopath($2-),0,1)) { .splay " $+ $findfile(%t,$nopath($2-),$rand(1,$ifmatch),1) $+ " }
  }
  elseif ($isfile($2-)) { .splay " $+ $_fixfilename($2-) $+ " }
}
alias ^playthemesound {
  if (%_sound) {
    var %tmp = ^sound. $+ $1,%s = $hget(_airctheme,%tmp),%ifmatch
    if ($inwave) || ($inmp3) || (%s == $null) { return }
    if (%s == <BEEP>) { .beep }
    elseif (\ isin %s) { _themesound $1 %s }
    else {
      if ($findfile($_fixdir($nofile($hget(_airctheme,currenthemefile))),%s,1,1)) { %ifmatch = $nofile(%ifmatch) $+ %s }
      else { %ifmatch = $wavedir $+ %s }
      _themesound $1 %ifmatch
      _hadd _airctheme %tmp $_fixfilename(%ifmatch,m)
      _themes.savehash
    }
  }
}
alias _theme.rini { return $readini -n " $+ %c.loadtheme.tfile $+ " $1 $2 }
alias _isvalidthemefile {
  var %t,%v
  %t = $readini -n " $+ $1- $+ " script ^theme
  %v = $readini -n " $+ $1- $+ " script ^themetype
  if (%t != $null) && (%v == 3) { return 1 }
  return 0
}
alias loadtheme {
  var %m = $_fixdir($_themesdir) $+ *.theme
  if ($1- == $null) { tokenize 32 $$dir="Select theme file:" %m }
  if ($isfile($1-) == $false) {
    error.a File not found: $^'($1-)
    return
  }
  if ($_not($_isvalidthemefile($1-))) {
    error.a Invalid themefile: $^'($1-)
    return
  }
  %c.loadtheme.tfile = $1-
  _progress.init Loading theme: $_theme.rini(script,^theme)
  _progress 0 Loading theme: $_theme.rini(script,^theme)
  if ($dialog(themes)) { %c.loadtheme.flags = $iif($did(themes,110).state,c) $iif($did(themes,111).state,b) $iif($did(themes,112).state,f) $iif($did(themes,115).state,w) $iif($did(themes,114).state,s) $iif($did(themes,113).state,t) }
  else { %c.loadtheme.flags = cbfst }
  hdel _airctheme ^onloadtheme
  _loadtheme $1-
  if ($hget(_airctheme,^onloadtheme) != $null) { .timerONLOADTHEME -o 1 0 $ifmatch }
  else { info.s Theme loaded: $^b($hget(_airctheme,currenthemename)) by $^b($hget(_airctheme,author)) }
  if ($dialog(themes)) { did -o themes 1001 1 Current theme: $hget(_airctheme,currenthemename) }
  ^playthemesound START
  saveini
  flushini " $+ $mircini $+ "
  unset %c.loadtheme.*
}
alias _loadtheme {
  var %a = $1-,%active = $active,%f = %c.loadtheme.flags,%file = $_usrdir(text.scheme)
  _progress 5 Loading theme header...
  if ($_getsetup(script,_unloadthemesbedoreloading)) {
    hdel -w _airctheme ^sound.*
    write -c " $+ %file $+ " ; Text Scheme
  }
  _hadd _airctheme author $_theme.rini(script,^author)
  _hadd _airctheme currenthemename $_theme.rini(script,^theme)
  _hadd _airctheme currenthemevers $_theme.rini(script,^themever)
  var %win = @__loadthemetmponload
  _openwins %win
  loadbuf -t $+ script %win " $+ %c.loadtheme.tfile $+ "
  if ($fline(%win,^onloadtheme=*,1)) { _hadd _airctheme ^onloadtheme $gettok($line(%win,$ifmatch),2-,$asc(=)) }
  close -@ %win
  _hadd _airctheme currenthemefile %a
  _themes.savehash
  _theme.color $_defrgbs
  if (c isin %f) {
    _progress 10 Loading colours...
    _theme.color $iif($_theme.rini(colours,rgbs),$replace($_theme.rini(colours,rgbs),$chr(32),$chr(44)),$null) $iif($_theme.rini(colours,colours),$replace($_theme.rini(colours,colours),$chr(32),$chr(44)),$null) $_theme.rini(colours,nicklist)
  }
  else { _progress 10 }
  if (b isin %f) {
    _progress 25 Loading backgrounds...
    _loadtheme.background
  }
  else { _progress 25 }
  if (f isin %f) {
    _progress 40 Loading fonts...
    _loadtheme.fonts
  }
  else { _progress 40 }
  if (w isin %f) {
    _progress 50 Loading windows sizes/positions...
    _loadtheme.windows
  }
  else { _progress 50 }
  if (s isin %f) {
    _progress 55 Loading sounds...
    _loadtheme.sounds
  }
  else { _progress 55 }
  if (t isin %f) && ($ini(%a,textheme)) {
    _progress 80 Loading texts...
    write -c $"(%file) ; Text Scheme - Theme: $_theme.rini(script,^theme)
    close -@ @_themes
    window -h @_themes
    loadbuf -ttextheme @_themes " $+ %a $+ "
    if ($line(@_themes,0)) { savebuf -a @_themes $"(%file) }
    close -@ @_themes
    if ($script(%file)) { .reload -rs9 $"(%file) }
    else { .load -rs9 $"(%file) }
  }
  else { _progress 80 }
  _progress 95 Theme created. Loading...
  if ($window(%active)) { window -a " $+ %active $+ " }
  _progress 100 Completed!
}
alias _loadtheme.background {
  if ($_getsetup(script,_unloadthemesbedoreloading)) { _unloadtheme.background }
  var %win = @__loadthemebackground,%if = %c.loadtheme.tfile,%l,%tok,%toks = @mdi toolbar toolbuttons switchbar status wfinger wmessage wchannel wquery wchat wserv
  if ($ini(%if,background)) {
    _openwins %win
    loadbuf -tbackground %win " $+ %if $+ "
    if ($line(%win,0)) {
      while ($gettok(%toks,1,32) != $null) {
        %tok = $ifmatch
        if ($fline(%win,$_nospaces(%tok,=*),1)) {
          %l = $ifmatch
          __loadtheme.background $line(%win,%l)
          dline %win %l
        }
        %toks = $deltok(%toks,1,32)
      }
      while ($line(%win,0)) {
        __loadtheme.background $line(%win,1)
        dline %win 1
      }
    }
    close -@ %win
  }
}
alias __loadtheme.background {
  tokenize 32 $gettok($1-,1,61) $gettok($1-,2-,61)
  if (%c.loadtheme.import) {
    if ($gettok($2-,$numtok($2-,44),44) isnum) { tokenize 32 $1 $ifmatch $deltok($2-,$numtok($2-,44),44) }
    else { tokenize 32 $1 f $2- }
    if (?:\* !iswm $3-) { tokenize 32 $1 $2 $nofile(%c.loadtheme.tfile) $+ $3- }
    tokenize 32 $1 $replace($2,0,c,1,f,2,n,3,r,4,t,5,p) $3-
  }
  elseif ($nopath($3-) == $3-) { tokenize 32 $1 $2 $nofile(%c.loadtheme.tfile) $+ $3- }
  _theme.background $iif($2,$2,-s) $1 $3-
}
alias _loadtheme.sounds {
  var %win = @__loadthemesounds,%if = %c.loadtheme.tfile,%i = 1,%n,%l,%tf
  if ($ini(%if,sounds)) {
    _openwins %win
    loadbuf -tsounds %win " $+ %if $+ "
    %n = $line(%win,0)
    while (%i <= %n) {
      %l = $line(%win,%i)
      _hadd _airctheme ^sound. $+ $upper($gettok(%l,1,61)) $_fixdir($nofile(%if),m) $+ $gettok(%l,2-,61)
      inc %i
    }
    close -@ %win
  }
  _themes.savehash
}
alias _loadtheme.windows {
  var %win = @__loadthemewindows,%if = %c.loadtheme.tfile,%l,%default,%tok,%toks = main scripts status wchannel wchat wdccg wdccs wfinger wlinks wlist wmessage wnotify wquery wserv wwwwlist
  if ($ini(%if,windows)) {
    _openwins %win
    loadbuf -twindows %win " $+ %if $+ "
    if ($line(%win,0)) {
      while ($gettok(%toks,1,32) != $null) {
        %tok = $ifmatch
        if ($fline(%win,$_nospaces(%tok,=*),1)) {
          %l = $ifmatch
          _theme.windows %tok $gettok($line(%win,%l),2-,61)
          dline %win %l
        }
        %toks = $deltok(%toks,1,32)
      }
      while ($line(%win,0)) {
        _theme.windows $gettok($line(%win,1),1,61) $gettok($line(%win,1),2-,61)
        dline %win 1
      }
    }
    close -@ %win
  }
}
alias _theme.windows {
  if ($remove($2,$chr(44),-) !isnum) { return }
  writeini " $+ $mircini $+ " windows $1-
  tokenize 32 $1 $replace($2,$chr(44),$chr(32))
  var %xy = $2 $4,%wh = $3 $5,%currentxy,%state = $6,%s = $replace(%state,0,-r,1,-n,2,-x),%i,%win
  if ($istok(main scripts,$1,32)) {  }
  elseif ($1 == status) {
    window "status window" %xy %wh
    window %s "status window"
  }
  elseif ($1 == wchannel) {
    %i = $chan(0)
    while (%i) {
      %win = $chan(%i)
      window " $+ %win $+ " $window(%win).x $window(%win).y %wh
      window %s " $+ %win $+ "
      dec %i
    }
  }
  elseif ($1 == wchat) {
    %i = $chat(0)
    while (%i) {
      %win = Chat $chat(%i)
      window " $+ %win $+ " $window(%win).x $window(%win).y %wh
      window %s " $+ %win $+ "
      dec %i
    }
  }
  elseif ($1 == wdccg) {
    %i = $get(0)
    while (%i) {
      %win = Get $get(%i) $get(%i).file
      window " $+ %win $+ " $window(%win).x $window(%win).y %wh
      window %s " $+ %win $+ "
      dec %i
    }
  }
  elseif ($1 == wdccs) {
    %i = $send(0)
    while (%i) {
      %win = Send $send(%i) $send(%i).file
      window " $+ %win $+ " $window(%win).x $window(%win).y %wh
      window %s " $+ %win $+ "
      dec %i
    }
  }
  elseif ($1 == wfinger) {
    if ($window(Finger Window)) {
      window "Finger Window" %xy %wh
      window %s "Finger Window"
    }
  }
  elseif ($1 == wlinks) {
    if ($window(Links List)) {
      window "Links List" %xy %wh
      window %s "Links List"
    }
  }
  elseif ($1 == wlist) {   }
  elseif ($1 == wmessage) {
    if ($window(Message Window)) {
      window "Message Window" %xy %wh
      window %s "Message Window"
    }
  }
  elseif ($1 == wnotify) {
    if ($window(Notify List)) {
      window "Notify List" %xy %wh
      window %s "Notify List"
    }
  }
  elseif ($1 == wquery) {
    %i = $query(0)
    while (%i) {
      %win = $query(%i)
      window " $+ %win $+ " $window(%win).x $window(%win).y %wh
      window %s " $+ %win $+ "
      dec %i
    }
  }
  elseif ($1 == wserv) {
    %i = $fserv(0)
    while (%i) {
      %win = Serve $fserv(%i)
      window " $+ %win $+ " $window(%win).x $window(%win).y %wh
      window %s " $+ %win $+ "
      dec %i
    }
  }
  elseif ($1 == wwwwlist) {
    if ($window(URL List)) {
      window "URL List" %xy %wh
      window %s "URL List"
    }
  }
  elseif ($_ischan($1)) {
    if ($chan($1)) {
      window " $+ $1 $+ " %xy %wh
      window %s " $+ $1 $+ "
    }
  }
  else {
    if ($query($1)) {
      window " $+ $1 $+ " %xy %wh
      window %s " $+ $1 $+ "
    }
    if ($chat($1,0)) {
      %win = Chat $1
      window " $+ %win $+ " %xy %wh
      window %s " $+ %win $+ "
    }
    if ($fserv($1,0)) {
      %win = Serve $1
      window " $+ %win $+ " %xy %wh
      window %s " $+ %win $+ "
    }
  }
}
alias _loadtheme.fonts {
  var %win = @__loadthemefonts,%if = %c.loadtheme.tfile,%l,%default,%tok,%toks = faliases fchannel fdccg fdccs fdccsg ffinger flinks flist fmessage fnotify fpopups fquery fremote fscripts fstatus fwwwlist
  if ($ini(%if,fonts)) {
    _openwins %win
    loadbuf -tfonts %win " $+ %if $+ "
    if ($line(%win,0)) {
      if ($fline(%win,fstatus=*,1)) { %default = $gettok($line(%win,$ifmatch),2-,61) }
      while ($gettok(%toks,1,32) != $null) {
        %tok = $ifmatch
        if ($fline(%win,$_nospaces(%tok,=*),1)) {
          %l = $ifmatch
          _theme.fonts %tok $gettok($line(%win,%l),2-,61)
          dline %win %l
        }
        else { _theme.fonts %tok %default }
        %toks = $deltok(%toks,1,32)
      }
      while ($line(%win,0)) {
        _theme.fonts $gettok($line(%win,1),1,61) $gettok($line(%win,1),2-,61)
        dline %win 1
      }
    }
    close -@ %win
  }
  elseif ($_getsetup(script,_unloadthemesbedoreloading)) { _unloadtheme.fonts }
}
; /_theme._fonts.quickcustomwin *match* flags font-
alias -l _theme._fonts.quickcustomwin {
  var %i = $window(0),%win,%b = - $+ $remove($2,-,d)
  if ($remove(%b,-,b) == $null) { var %b }
  while (%i) {
    %win = $window(%i)
    if ($1 iswm %win) { font %b %win $3- }
    dec %i
  }
}
alias _theme.fonts {
  var %n,%b = -d,%f = $gettok($2-,1,44),%t = $gettok($2-,2,44),%win
  if (%f == $null) || (%t !isnum) { return }
  writeini " $+ $mircini $+ " fonts $1-
  if (%t > 700) {
    dec %t 700
    %b = -bd
  }
  if (%t > 400) { dec %t 400 }
  if ($1 == fstatus) {
    font $iif(b isin %b,-sb,-s) %t %f
    _theme._fonts.quickcustomwin @Status* %b %t %f
  }
  elseif ($1 == fchannel) {
    %n = $chan(0)
    while (%n) {
      font %b $chan(%n) %t %f
      dec %n
    }
    _theme._fonts.quickcustomwin @CH_* %b %t %f
  }
  elseif ($1 == fquery) {
    %n = $query(0)
    while (%n) {
      font %b $query(%n) %t %f
      dec %n
    }
    %n = $chat(0)
    while (%n) {
      %win = Chat $chat(%n)
      _dummy $__theme.font(%win,%t,%f)
      dec %n
    }
    %n = $fserv(0)
    while (%n) {
      %win = Serve $fserv(%n)
      _dummy $__theme.font(%win,%t,%f)
      dec %n
    }
    _theme._fonts.quickcustomwin @Q_* %b %t %f
    _theme._fonts.quickcustomwin @DCC CHAT* %b %t %f
  }
  elseif ($istok(fdccs fdccg fdccsg,$1,32)) {
    if ($1 == fdccs) || ($1 == fdccsg) {
      %n = $send(0)
      while (%n) {
        %win = Send $send(%n) $send(%n).file
        _dummy $__theme.font(%win,%t,%f)
        dec %n
      }
      _theme._fonts.quickcustomwin @DCC SEND* %b %t %f
    }
    if ($1 == fdccg) || ($1 == fdccsg) {
      %n = $get(0)
      while (%n) {
        %win = Get $get(%n) $get(%n).file
        _dummy $__theme.font(%win,%t,%f)
        dec %n
      }
      _theme._fonts.quickcustomwin @DCC GET* %b %t %f
    }
  }
  elseif ($1 == ffinger) { _dummy $__theme.font(Finger Window,%t,%f) }
  elseif ($1 == flinks) { _dummy $__theme.font(Links List,%t,%f) }
  elseif ($1 == fmessage) { _dummy $__theme.font(Message Window,%t,%f) }
  elseif ($1 == fnotify) { _dummy $__theme.font(Notify List,%t,%f) }
  elseif ($1 == fwwwlist) { _dummy $__theme.font(URL List,%t,%f) }
  elseif ($istok(faliases flist fpopups fremote fscripts,$1,32)) {  }
  elseif (f* iswm $1) && ($_ischan($right($1,-1))) {
    if (b isin %b) { %b = -b }
    else { var %b }
    if ($chan($right($1,-1))) { font %b $ifmatch %t %f }
    _theme._fonts.quickcustomwin @CH_*  $+ $right($1,-1) $iif(%b,-b,-) %t %f
  }
}
; $__theme.font(mirc window,size,font,1=back to active,0=don't)
alias __theme.font {
  var %active = $active,%state
  if ($window($1)) {
    %state = $window($1).state
    window -a " $+ $1 $+ "
    font -a $2 $3
    if (%state == minimized) { window -n " $+ $1 $+ " }
    elseif (%state == hidden) { window -h " $+ $1 $+ " }
    if ($window(%active)) && ($4) { window -a " $+ %active $+ " }
  }
}
alias unloadtheme {
  _theme.default
  info.s Themes restored.
  _hadd _airctheme author
  _hadd _airctheme currenthemename
  _hadd _airctheme currenthemevers
  _themes.savehash
  if ($dialog(themes)) { did -o themes 1001 1 No theme loaded }
  saveini
  flushini " $+ $mircini $+ "
}
alias _theme.default {
  var %active = $active,%file = $_usrdir(text.scheme)
  _progress.init Restoring default theme...
  _progress 0 Restoring default theme...
  hdel -w _airctheme ^sound.*
  _themes.savehash
  _progress 10 Restoring colours...
  _unloadtheme.color
  saveini
  flushini " $+ $mircini $+ "
  _progress 25 Restoring fonts...
  _unloadtheme.fonts
  _progress 40 Restoring backgrounds...
  _unloadtheme.background
  _progress 55 Loading...
  write -c $"(%file) ; Text Scheme
  .load -rs9 $"(%file)
  if ($window(%active)) { window -a " $+ %active $+ " }
  if ($dialog(themes)) { dialog -v themes }
  _progress 100 Completed!
}
alias _unloadtheme.color { _theme.color $_defrgbs 0,6,4,5,2,3,3,3,3,3,3,1,5,7,6,1,3,2,3,5,1,0,1,0,1 $replace($_nickol.whitedefault,$chr(32),$chr(44)) }
alias _unloadtheme.fonts {
  remini " $+ $mircini $+ " fonts
  _theme.fonts faliases Lucida Console,411
  _theme.fonts fchannel Lucida Console,411
  _theme.fonts fdccg Lucida Console,410
  _theme.fonts fdccs Lucida Console,410
  _theme.fonts fdccsg Lucida Console,411
  _theme.fonts ffinger Lucida Console,410
  _theme.fonts flinks Lucida Console,410
  _theme.fonts flist Lucida Console,410
  _theme.fonts fmessage Lucida Console,411
  _theme.fonts fnotify Lucida Console,411
  _theme.fonts fpopups Lucida Console,411
  _theme.fonts fquery Lucida Console,411
  _theme.fonts fremote Lucida Console,411
  _theme.fonts fscripts Lucida Console,414
  _theme.fonts fstatus Lucida Console,411
  _theme.fonts fwwwlist Lucida Console,410
}
alias _unloadtheme.background {
  remini " $+ $mircini $+ " background
  _theme.background -s @mdi
  _theme.background $null toolbar
  _theme.background $null toolbuttons
  _theme.background $null switchbar
  _theme.background -s status
  _theme.background -s wfinger
  _theme.background -s wmessage
  _theme.background -s wchannel
  _theme.background -s wquery
  _theme.background -s wchat
  _theme.background -s wserv
}
alias _theme.color {
  if ($numtok($1,44) == 16) {
    var %c = 0,%from,%to
    while (%c <= 15) {
      var %rgb = $colour(%c),%from = $rgb($gettok(%rgb,1,44),$gettok(%rgb,2,44),$gettok(%rgb,3,44)),%to = $gettok($1,$calc(%c + 1),44)
      if (%to isnum) && (%from != %to) { colour %c %to }
      inc %c
    }
  }
  if ($numtok($2,44) == 25) {
    var %c = 1,%item,%from,%to
    while (%c <= 25) {
      var %item = $gettok(background.action text.ctcp text.highlight text.info text.info2 text.invite text.join text.kick text.mode text.nick text.normal text.notice text.notify text.other text.own text.part text.quit text.topic text.wallops text.whois text.editbox.editbox text.listbox.listbox text,%c,46),%from = $colour(%item),%to = $gettok($2,%c,44)
      if (%to isnum) && (%from != %to) { colour %item %to }
      inc %c
    }
  }
  if ($numtok($3,44) == 16) {
    var %old = $_getsetup(script,_nicklistcol.cols)
    _setsetup script _nicklistcol.cols $replace($3,$chr(44),$chr(32))
    _loadsetup.nicklistcol
    if ($_getsetup(script,_nicklistcol.state)) && (%old != $_nickol.fix) { _nicklist.col.all }
  }
}
alias _theme.background {
  var %1,%win,%wtt,%n,%a = $3-,%wini = writeini " $+ $mircini $+ ",%rmini = remini " $+ $mircini $+ "
  if ($1 != $null) {
    %1 = $remove($1,-,a,m,s,g,d,e,i,u,h)
    %n = $replace(%1,c,0,f,1,n,2,r,3,t,4,p,5)
  }
  if ($isfile(%a) == $false) { var %a }
  if ($2 == @mdi) {
    if (%a != $null) { background -em $+ %1 " $+ $ifmatch $+ " }
    else { background -emx }
  }
  elseif ($2 == status) {
    if (%a != $null) { background -es $+ %1 " $+ $ifmatch $+ " }
    else { background -esx }
  }
  elseif ($2 == switchbar) {
    if (%a != $null) { background -eh " $+ $ifmatch $+ " }
    else { background -ehx }
  }
  elseif ($2 == toolbar) {
    if (%a != $null) { background -el " $+ $ifmatch $+ " }
    else { background -elx }
  }
  elseif ($2 == toolbuttons) {
    if (%a != $null) { background -eu " $+ $ifmatch $+ " }
    else { background -eux }
  }
  elseif ($2 == wchannel) {
    if ($chan(0)) {
      var %c = $ifmatch
      while (%c) {
        if (%a != $null) { background -e $+ %1 $chan(%c) " $+ $ifmatch $+ " }
        else { background -ex $chan(%c) }
        dec %c
      }
    }
    elseif (%a != $null) { %wini background wchannel $ifmatch $+ , $+ %n }
    else { %rmini background wchannel }
  }
  elseif ($2 == wserv) {
    if ($fserv(0)) {
      var %s = $ifmatch
      while (%s) {
        if (%a != $null) { background -e $+ %1 = $+ $fserv(%s) " $+ $ifmatch $+ " }
        else { background -ex = $+ $fserv(%s) }
        dec %s
      }
    }
    elseif (%a != $null) { %wini background wserv $ifmatch $+ , $+ %n }
    else { %rmini background wserv }
  }
  elseif ($2 == wchat) {
    if ($chat(0)) {
      var %q = $ifmatch
      while (%q) {
        if (%a != $null) { background -e $+ %1 = $+ $chat(%q) " $+ $ifmatch $+ " }
        else { background -ex = $+ $chat(%q) }
        dec %q
      }
    }
    elseif (%a != $null) { %wini background wchat $ifmatch $+ , $+ %n }
    else { %rmini background wchat }
  }
  elseif ($2 == wfinger) {
    if ($window(Finger Window)) {
      if (%a != $null) { background -eg $+ %1 " $+ $ifmatch $+ " }
      else { background -egx }
    }
    elseif (%a != $null) { %wini background wfinger $ifmatch $+ , $+ %n }
    else { %rmini background wfinger }
  }
  elseif ($2 == wmessage) {
    if ($window(Message Window)) {
      if (%a != $null) { background -ed $+ %1 " $+ $ifmatch $+ " }
      else { background -edx }
    }
    elseif (%a != $null) { %wini background wmessage $ifmatch $+ , $+ %n }
    else { %rmini background wmessage }
  }
  elseif ($2 == wquery) {
    if ($query(0)) {
      var %q = $ifmatch
      while (%q) {
        if (%a != $null) { background -e $+ %1 $query(%q) " $+ $ifmatch $+ " }
        else { background -ex $query(%q) }
        dec %q
      }
    }
    elseif (%a != $null) { %wini background wquery $ifmatch $+ , $+ %n }
    else { %rmini background wquery }
  }
  elseif ($_ischan($2)) {
    if ($chan($2)) {
      if (%a != $null) { background - $+ %1 $2 " $+ $ifmatch $+ " }
      else { background -x $2 }
    }
    elseif (%a != $null) { %wini background $2 $ifmatch $+ , $+ %n }
    else { %rmini background $2 }
  }
  else {
    if ($query($2)) || ($chat($2,0)) || ($serv($2,0)) {
      if (%a != $null) {
        if ($query($2)) { background - $+ %1 $2 " $+ %a $+ " }
        if ($chat($2)) || ($serv($2)) { background - $+ %1 = $+ $2 " $+ %a $+ " }
      }
      else {
        if ($query($2)) { background -x $2 }
        if ($chat($2)) || ($serv($2)) { background -x = $+ $2 }
      }
    }
    elseif (%a != $null) { %wini background $2 $ifmatch $+ , $+ %n }
    else { %rmini background $2 }
  }
}
alias savetheme { _dialog -am savetheme savetheme }
alias _savetheme {
  .splay stop
  var %f,%d,%n = $did(savetheme,11),%v = $did(savetheme,16),%a = $did(savetheme,21),%_ = $replace($mkfn(%n),.,_,$chr(44),_),%tmpdir = $_tmpdir($_nospaces($_randstring(15),\)),%tmpfile = %tmpdir $+ %_ $+ .theme,%color = $did(savetheme,110).state,%background = $did(savetheme,111).state,%fonts = $did(savetheme,112).state,%text = $did(savetheme,113).state,%sound = $did(savetheme,114).state,%winpos = $did(savetheme,115).state,%file = $_usrdir(text.scheme)
  %d = $mircdir $+ themes\ $+ %_ $+ \
  %f = %d $+ %_ $+ .theme
  %c.savetheme.dir = %d
  %c.savetheme.tempdir = %tmpdir
  tokenize 32 %tmpfile
  saveini
  dialog -x savetheme
  if ($isdir(%d)) || ($isfile($1-)) {
    if ($_not($_yesno(1,Themes [/themes],1,The theme already exists. Do you want to replace it?))) {
      unset %c.savetheme.*
      return
    }
  }
  _progress.init Saving theme
  _progress 0 Creating temp dir
  ._rmdir %tmpdir
  .mkdir " $+ %tmpdir $+ "
  write -c " $+ $1- $+ "
  _progress 2 Saving header
  .writeini " $+ $1- $+ " script ^author %a
  .writeini " $+ $1- $+ " script ^theme %n
  if (%v != $null) { .writeini " $+ $1- $+ " script ^themever $ifmatch }
  flushini " $+ $1- $+ "
  if ($hget(_airctheme,^onloadtheme) != $null) { write " $+ $1- $+ " ^onloadtheme= $+ $ifmatch }
  .writeini " $+ $1- $+ " script ^themetype 3
  if (%color) {
    _progress 10 Saving colours
    .writeini " $+ $1- $+ " colours colours $_mini(colours,n0)
    if ($_getsetup(script,_nicklistcol.state)) { .writeini " $+ $1- $+ " colours nicklist $replace($_getsetup(script,_nicklistcol.cols),$chr(32),$chr(44)) }
    var %coli = 0,%coln = 15,%rgbs
    while (%coli <= %coln) {
      %rgbs = %rgbs $_mircol2rgbnum(%coli)
      inc %coli
    }
    .writeini " $+ $1- $+ " colours rgbs $replace(%rgbs,$chr(32),$chr(44))
  }
  else { _progress 10 }
  saveini
  if (%background) {
    _progress 25 Saving backgrounds
    _savetheme.background $1-
  }
  else { _progress 25 }
  if (%fonts) {
    _progress 40 Saving fonts
    _savetheme.fonts $1-
  }
  else { _progress 40 }
  if (%winpos) {
    _progress 50 Saving windows sizes/positions...
    _savetheme.windows $1-
  }
  else { _progress 50 }
  if (%sound) {
    _progress 55 Saving sounds
    _savetheme.sounds $1-
  }
  else { _progress 55 }

  var %twin = @__themesavetextscheme,%ti,%tl
  _openwins %twin
  aline %twin [textheme]
  loadbuf -a %twin " $+ %file $+ "
  if (; Text Scheme* iswm $line(%twin,2)) { dline %twin 2 }
  %ti = $line(%twin,0)
  while (%ti > 1) {
    %tl = $line(%twin,%ti)
    if ($remove(%tl,$chr(32),$chr(9)) == $null) { dline %twin %ti }
    elseif ([?*] iswm %tl) { rline %twin %ti ; $+ %tl }
    dec %ti
  }
  if ($line(%twin,0) > 1) {
    savebuf -a %twin " $+ $1- $+ "
    _progress 80 Saving texts
  }
  else { _progress 80 }
  close -@ %twin

  _progress 90 Copying to $_fixdir(%d,m)
  ._rmdir %d
  .mkdir " $+ %d $+ "
  _dummy $_xcopy(%tmpdir,%c.savetheme.dir,*,_)
  ._rmdir %tmpdir
  _progress 100 Done!
  if ($dialog(themes)) { _theme.loadlist }
  unset %c.savetheme.*
}
alias _savetheme.copydir { .copy -o " $+ $1- $+ " " $+ %c.savetheme.dir $+ " }
alias _savetheme.copytempdir { .copy -o " $+ $1- $+ " " $+ %c.savetheme.tempdir $+ " }
alias _savetheme.fonts {
  var %win = @__savethemefonts
  if ($ini($mircini,fonts)) {
    close -@ %win
    window -hs %win
    loadbuf -tfonts %win " $+ $mircini $+ "
    while ($line(%win,0)) {
      writeini " $+ $1- $+ " fonts $gettok($line(%win,1),1,61) $gettok($line(%win,1),2-,61)
      dline %win 1
    }
  }
  close -@ %win
}
alias _savetheme.sounds {
  .splay stop
  var %i = 1,%n = $hget(_airctheme,0).item,%a,%t
  while (%i <= %n) {
    %a = $hget(_airctheme,%i).item
    if (^sound.?* iswm %a) {
      %t = $hget(_airctheme,%a)
      %t = $_fixdir($nofile(%t)) $+ $nopath(%t)
      .writeini " $+ $1- $+ " sounds $right(%a,-7) $nopath(%t)
      _dummy $findfile($nofile(%t),$nopath(%t),0,_savetheme.copytempdir $1-)
    }
    inc %i
  }
}
alias _savetheme.windows {
  var %win = @__savethemewindows
  if ($ini($mircini,windows)) {
    close -@ %win
    window -hs %win
    loadbuf -twindows %win " $+ $mircini $+ "
    while ($line(%win,0)) {
      writeini " $+ $1- $+ " windows $gettok($line(%win,1),1,61) $gettok($line(%win,1),2-,61)
      dline %win 1
    }
  }
  close -@ %win
}
alias _savetheme.background {
  var %win = @__savethemebackground,%t,%f,%m,%c,%a
  if ($ini($mircini,background)) {
    _openwins %win
    loadbuf -tbackground %win " $+ $mircini $+ "
    while ($line(%win,0)) {
      %c = $gettok($line(%win,1),1,61)
      %t = $gettok($line(%win,1),2-,61)
      if ($gettok(%t,$numtok(%t,44),44) isnum) {
        %m = $ifmatch
        %a = $deltok(%t,$numtok(%t,44),44)
      }
      else { %a = %t }
      if (%m != $null) { %m = $replace(%m,0,c,1,f,2,n,3,r,4,t,5,p) }
      else { %m = - }
      %a = $_fixfilename(%a)
      _dummy $findfile($nofile(%a),$nopath(%a),0,_savetheme.copytempdir $1-)
      %a = $nopath(%a)
      if (%a != $null) { .writeini " $+ $1- $+ " background %c %m %a }
      dline %win 1
    }
    close -@ %win
  }
}
dialog themes {
  size -1 -1 203 182
  option dbu
  title "Themes"

  text "",1001,5 5 110 10
  text "Select a theme...",1002,8 108 188 10

  list 100,8 21 188 95,sort
  list 101,8 21 188 95,sort hide

  box "",105,5 15 195 104
  box "",106,5 15 195 132
  box "",107,5 15 195 147

  check "C&olours",110,8 120 30 15
  check "&Backgrounds",111,45 120 45 15
  check "&Fonts",112,95 120 65 15
  check "&Texts",113,8 132 30 15
  check "So&unds",114,45 132 45 15
  check "&Windows Position/Size",115,95 132 65 15

  button "&Load",230,165 126 33 15,ok

  check "Text Schemes",231,8 150 45 10
  check "Restore before loading",232,95 150 80 10

  button "&Restore",233,53 165 33 15
  button "&Import",234,90 165 33 15
  button "&Save",237,127 165 33 15
  button "&Close",238,164 165 33 15,cancel
}
ON *:DIALOG:themes:init:0:{
  _mdx.init $_icofile(themes.ico)
  var %9+0 = $chr(9) $+ +l 0,%9r0 = $chr(9) $+ +r 0
  _mdx.SetControlMDX views.mdx $dname 100 ListView headerdrag report rowselect showsel nosortheader single
  did -i $dname 100 1 header $_mdx.fixtablengths(80,25,35,35,45,90) + 0 Theme $+ %9r0 Version $+ %9+0 Author $+ %9+0 Background $+ %9+0 Font $+ %9+0 File
  _theme.loadlist
  if ($hget(_airctheme,currenthemename) != $null) { did -o themes 1001 1 Current theme: $ifmatch }
  else { did -o themes 1001 1 No theme loaded }
  did -c themes 110,111,112,113,114
  did $iif($_getsetup(script,_texthemes),-c,-u) $dname 231
  did $iif($_getsetup(script,_unloadthemesbedoreloading),-c,-u) $dname 232
  _dummy $_lang.loaddialog($dname,_themes,00086969,main.lang,$scriptdir)
}
ON *:DIALOG:themes:dclick:*:{ dialog -k $dname }
ON *:DIALOG:themes:sclick:100:{
  var %last = $gettok($did($dname,100,1),1,32),%sel = $did($dname,100,1).sel,%tmp,%name,%file,%desc
  if (%last == item) && (%sel) {
    var %tmp = $did($dname,100,%sel),%name = $gettok($gettok(%tmp,1,9),3-,32),%file = $gettok($gettok(%tmp,6-,9),2-,32),%desc = $_readini(%file,script,^description)
    did -o $dname 1002 1 $iif(%desc != $null,%desc,%name)
  }
}
ON *:DIALOG:themes:sclick:231:{
  _setsetup script _texthemes $did($dname,231).state
  _start.themes
}
ON *:DIALOG:themes:sclick:232:{ _setsetup script _unloadthemesbedoreloading $did($dname,232).state }
ON *:DIALOG:themes:sclick:237:{ savetheme }
ON *:DIALOG:themes:sclick:233:{ unloadtheme }
ON *:DIALOG:themes:sclick:234:{ importheme }
ON *:DIALOG:themes:sclick:230:{
  if ($did(100).sel) { loadtheme $gettok($gettok($did(themes,100,$did(themes,100).sel),6-,9),2-,32) }
  else { halt }
}
alias -l _theme.loadlist {
  did -r themes 100
  var %a = $findfile($_fixdir(themes\),*.theme,0,_theme.loadlistfile $1-)
  did -z themes 100
}
alias -l _theme.loadlistfile {
  var %name = $remove($_readini($1-,script,^theme),$chr(9)),%themetype = $remove($_readini($1-,script,^themetype),$chr(9))
  if (%name == $null) || (%themetype != 3) { return }
  var %90 = $chr(9) $+ 0,%version = $remove($_readini($1-,script,^themever),$chr(9)),%author = $remove($_readini($1-,script,^author),$chr(9)),%background = $remove($_readini($1-,script,^background),$chr(9)),%font = $remove($_readini($1-,script,^themefont),$chr(9)),%file = $_fixfilename($1-,m)
  if (%background == $null) { %background = unknown }
  if (%font == $null) { %font = unknown }
  did -a themes 100 + 0 %name $+ %90 %version $+ %90 %author $+ %90 %background $+ %90 %font $+ %90 %file
}
dialog savetheme {
  size -1 -1 170 109
  title "Save Theme"
  option dbu

  text "Theme name:",10,5 5 70 10
  edit "",11,10 15 70 10,autohs
  text "Version [optional]:",15,5 30 70 10
  edit "",16,10 40 70 10,autohs
  text "Author:",20,5 55 70 10
  edit "",21,10 65 70 10,autohs

  box "Include:",100,85 5 80 80
  check "C&olours",110,90 15 70 15
  check "&Backgrounds",111,90 25 70 15
  check "&Fonts",112,90 35 70 15
  check "&Texts",113,90 45 70 15
  check "So&unds",114,90 55 70 15
  check "&Windows Position/Size",115,90 65 70 15

  button "&Save",230,30 90 35 15,default
  button "&Close",240,90 90 35 15,cancel
}
ON *:DIALOG:savetheme:init:0:{
  _mdx.init $_icofile(themes.ico)
  did -c $dname 110,111,112,113,114
  if ($hget(_airctheme,currenthemename) != $null) { did -o $dname 11 1 $ifmatch }
  if ($hget(_airctheme,currenthemevers) != $null) { did -o $dname 16 1 $ifmatch }
  if ($hget(_airctheme,author) != $null) { did -o $dname 21 1 $ifmatch }
  else { did -o $dname 21 1 $me }
  _dummy $_lang.loaddialog($dname,_savetheme,00086969,main.lang,$scriptdir)
}
ON *:DIALOG:savetheme:sclick:230:{ _savetheme }
alias importheme {
  var %mircini = $1-,%todo
  if (%mircini == $null) { %mircini = $$dir="Select mirc.ini" $left($mircdir,3) $+ mirc.ini }
  if ($isfile(%mircini) == $false) {
    error.a File not found: $^'(%mircini)
    return
  }
  if ($nopath(%mircini) != mirc.ini) {
    error.a Invalid mirc.ini file: $^'(%mircini)
    return
  }
  if ($dialog(themes)) { dialog -v themes }
  %todo = $$dialog(importheme,importheme,-4)
  %c.loadtheme.tfile = %mircini
  %c.loadtheme.import = 1
  if (b isin %todo) {
    _unloadtheme.background
    _loadtheme.background
  }
  if (f isin %todo) { _loadtheme.fonts }
  if (w isin %todo) { _loadtheme.windows }
  if (c isin %todo) { _theme.color $_defrgbs $readini -n " $+ %mircini $+ " colours n0 }
  unset %c.loadtheme.*
}
dialog importheme {
  size -1 -1 88 87
  title "Import 'theme' mirc.ini"
  option dbu

  box "Import:",100,5 5 80 60
  check "C&olours",110,10 15 70 15
  check "&Backgrounds",111,10 25 70 15
  check "&Fonts",112,10 35 70 15
  check "&Windows Position/Size",113,10 45 70 15

  button "&Import",230,5 70 35 15,ok
  button "&Cancel",240,50 70 35 15,cancel
  edit "",250,100 100 100 100,autohs result
}
ON *:DIALOG:importheme:init:0:{
  _mdx.init $_icofile(themes.ico)
  did -c $dname 110,111,112,113
  _dummy $_lang.loaddialog($dname,_importheme,00086969,main.lang,$scriptdir)
}
ON *:DIALOG:importheme:sclick:230:{ did -o $dname 250 1 $iif($did(110).state,c) $iif($did(111).state,b) $iif($did(112).state,f) $iif($did(113).state,w) }