mIRC Dialog Studio — File Browser

mIRCDialogStudiov1.1 / mIRCDialogStudiov1.1 / mIRC Dialog Studio / mIRC Dialog Studio.mrc

mIRC Dialog Studio.mrc — 113.11 KB — Download this file

/*
mIRC Dialog Studio for mIRC v7.63+

Version:  1.1
Coded by: Dazuz @ QuakeNet

Dependencies:
DCX.DLL 2.0.1120+


1.1 (2020-09-08)
- Cleanup on mIRC exit, if MDS is open.
- Fixed auto width checkbox not being checked/unchecked on control selection.
- Fixed a problem with captions that contain comma(s) when importing dialogs.
- Fixed dialog width/height resize issue.
- Added support for group box left/right/center style. (mIRC 7.63+)

1.0.1 (2020-03-02)
- Added mIRC version check to on load event, and set the mIRC version requirement to 7.61.
- Added noqt check for title during dialog import.

1.0 (2019-12-14)
- Initial release.
*/

;This alias creates $scriptdirCube.ico file. Feel free to remove the alias, it won't break anything important.
alias mds.createicon {
  set -l %icon $qt($scriptdirCube.ico)
  if (!$isfile(%icon)) {
    bset -t & 1 Y2BgBEIBAQYgUGDIYGFgEAOyNIBYACzCAJQFkQwMDiwMGOD///+MtMBrt+9oRMak6steN+U/MiZkDrq+zMPL/6c/PPSfGHOw6YNhdP3I5sD0Y9NHSD8II+vHpnco63/x8+v/vS/uwjGx+kH6YBhZPzZzkPXPO7sHRS8u/SDce2wTin6YGejm4NKHrheXOaTow2UOqfpIxaASgBIMAA==
    if ($decode(&,bm)) && ($decompress(&,bm1)) && ($sha1(&,1) == ae378536e1f7af22066cb722dd04739a5a7fe8a4) bwrite %icon 0 &
  }
}

on *:load: {
  if ($version < 7.63) {
    echo $color(info) -at * Unsupported mIRC version, please update to $v2 or newer.
    unload -rs $qt($script)
  }
  else {
    echo $color(info) -at * mIRC Dialog Studio loaded! Type /MDS to open mIRC Dialog Studio.
    mds.createicon
  }
}

on *:unload: {
  if ($com(mds)) .comclose mds
  if ($fopen(mds)) .fclose mds
  set -l %x 1
  while ($dialog(%x)) {
    if (mds.* iswm $v1) dialog -x $v2
    else inc %x
  }
  hfree -w mds.*
  while ($window(@mds.*,1)) window -c $v1
  window -c @MDS
  unset %mds.*
  .timermds.* off
  .remove $qt($scriptdirCube.ico)
  .remove $qt($scriptdirMDS.tmp)
  echo $color(info) -at * mIRC Dialog Studio unloaded! If you want to remove the config file, type: /remove $qt($scriptdirMDS.db)
}

menu menubar {
  mIRC Dialog Studio
  .$+(mIRC Dialog Studio,$chr(9),/mds):mds
  .-
  .Uninstall:if ($input(Do you really want to uninstall mIRC Dialog Studio?,yqu,mIRC Dialog Studio)) unload -rs $qt($script)
}

dialog -l mds {
  title "mIRC Dialog Studio - /mds"
  icon $scriptdirCube.ico
  size 10 200 10000 10000
  option map
  text "Project:", 1, 7 6 33 10, right
  combo 2, 44 5 163 246, sort size drop
  text "Control:", 3, 7 22 33 10, right
  combo 4, 44 21 163 246, size drop
  button "", 5, 193 38 13 12, default hide
  text "", 6, 181 39 11 10, hide
  list 198, 211 5 48 238, size

  tab "Properties", 100, 7 36 200 208
  text "Caption:", 101, 10 62 43 10, tab 100 right
  edit "", 102, 57 61 141 12, tab 100 autohs limit 8192
  check "No quotes around the caption", 103, 57 76 131 12, tab 100

  text "XYWH:", 104, 10 92 43 10, tab 100 right
  ;edit "", 105, 57 90 33 12, tab 100 center limit 5
  ;edit "", 106, 93 90 33 12, tab 100 center limit 5
  ;edit "", 107, 129 90 33 12, tab 100 center limit 5
  ;edit "", 108, 165 90 33 12, tab 100 center limit 5

  text "ID:", 109, 10 108 43 10, tab 100 right hide
  ;edit "", 110, 57 106 33 12, tab 100 center limit 5 hide
  text "", 111, 92 108 107 10, tab 100 center hide

  text "Table name:", 112, 10 108 43 10, tab 100 right
  edit "", 113, 57 106 141 12, tab 100 autohs limit 8192

  text "Icon:", 114, 10 124 43 10, tab 100 right
  edit "", 115, 57 122 105 12, tab 100 autohs limit 8192
  ;edit "", 116, 165 122 33 12, tab 100 autohs center limit 12

  text "Alignment:", 117, 10 124 43 10, tab 100 right hide
  combo 118, 57 122 68 246, tab 100 size drop hide

  text "Tab:", 119, 10 141 43 10, tab 100 right hide
  combo 120, 57 140 68 246, tab 100 size drop hide

  text "Styles:", 121, 10 157 43 10, tab 100 right hide
  check "disable", 122, 57 156 40 12, tab 100 hide
  check "group", 123, 57 168 40 12, tab 100 hide
  check "hide", 124, 57 180 40 12, tab 100 hide
  check "result", 125, 57 192 40 12, tab 100 hide

  text "Range:", 126, 10 208 43 10, tab 100 right hide
  ;edit "", 127, 57 207 32 12, tab 100 autohs center limit 12 hide
  ;edit "", 128, 93 207 33 12, tab 100 autohs center limit 12 hide

  text "Limit:", 129, 10 208 43 10, tab 100 right hide
  ;edit "", 130, 57 207 33 12, tab 100 autohs center limit 12 hide

  list 131, 129 142 69 95, tab 100 sort size check

  text "Tabs:", 132, 10 108 43 10, tab 100 right hide
  list 133, 57 106 68 130, tab 100 size hide
  button "Add", 134, 128 189 35 15, tab 100 hide
  button "Delete", 135, 164 189 35 15, tab 100 hide
  text "Caption:", 136, 129 108 29 10, tab 100 hide
  edit "", 137, 129 119 69 12, tab 100 autohs limit 8192 hide
  check "No quotes", 138, 129 133 49 12, tab 100 hide
  text "ID:", 139, 129 160 12 10, tab 100 hide
  ;edit "", 140, 144 159 33 12, tab 100 center limit 12 hide
  text "", 141, 128 148 73 10, tab 100 center hide
  link "Help", 142, 181 160 15 10, tab 100
  check "Use for children", 143, 129 174 65 12, tab 100

  text "Alignment:", 150, 10 208 43 10, tab 100 right hide
  combo 151, 57 207 68 246, tab 100 size drop hide
  text "Size:", 152, 10 226 43 10, tab 100 right hide
  combo 153, 57 224 68 246, tab 100 size drop hide

  text "Base unit:", 160, 10 140 43 10, tab 100 right
  check "map", 161, 57 139 28 11, tab 100

  check "Auto width", 197, 57 205 51 12, tab 100 hide

  text "", 199, 192 136 11 10, tab 100 hide

  tab "Menu Editor", 200
  ;list 201, 14 62 121 176, tab 200 size
  button "New menu", 202, 140 62 57 15, tab 200
  button "New item", 203, 140 77 57 15, tab 200 disable
  button "Delete", 204, 140 92 57 15, tab 200 disable
  button "Move up", 205, 140 112 57 15, tab 200 disable
  button "Move down", 206, 140 127 57 15, tab 200 disable
  text "ID:", 207, 138 189 12 10, tab 200 disable
  ;edit "", 208, 153 188 33 12, tab 200 limit 6 disable
  text "Styles:", 209, 138 216 24 10, tab 200 disable
  check "ok", 210, 165 215 27 12, tab 200 disable
  check "cancel", 211, 165 227 36 12, tab 200 disable
  text "", 212, 137 175 67 10, tab 200 center
  check "No quotes", 213, 138 202 48 12, tab 200 disable

  tab "Settings", 300
  text "Begin with ID:", 301, 13 62 51 10, tab 300 right
  ;edit "", 302, 66 61 33 12, tab 300 autohs center limit 8192
  text "Tab type:", 303, 13 79 51 10, tab 300 right
  combo 304, 66 78 93 62, tab 300 size drop
  link "Help", 305, 165 79 15 10, tab 300
  text "Tab WH:", 306, 13 96 51 10, tab 300 right
  ;edit "", 307, 66 95 33 12, tab 300 center limit 5
  ;edit "", 308, 102 95 33 12, tab 300 center limit 5
  text "Pixels", 311, 139 97 21 11, tab 300
  text "Default control values (doubleclick to edit):", 309, 13 111 139 10, tab 300
  list 310, 13 122 187 117, tab 300 size

  menu "File", 400
  item "New project", 401, 400
  item "Close project", 402, 400
  item break, 403, 400
  item "Recent", 404, 400
  item break, 405, 400
  item "Exit", 406, 400

  menu "Import", 500
  item "From clipboard...", 501, 500
  item "From file...", 502, 500

  menu "Export", 600
  item "To clipboard", 601, 600
  item "To file...", 602, 600
  item break, 603, 600
  item "View code", 604, 600
  item "Preview dialog", 605, 600

  menu "Help", 900
  item "Help", 901, 900
  item break, 902, 900
  item "About", 903, 900
}

alias mds {
  if ($dll(dcx.dll)) %mds.dcx = $qt($v1)
  else %mds.dcx = $qt($findfile($mircdir,dcx.dll,1))
  if ($isfile(%mds.dcx)) {
    if ($dialog(mds)) dialog -v mds
    else dialog -dm mds mds
  }
  else echo $color(info) * mIRC Dialog Studio: unable to locate DCX.DLL in: $mircdir
}

on *:dialog:mds:init:0: {
  dll %mds.dcx Mark $dname mds.callback
  dll %mds.dcx xdialog mds -c 99 statusbar 0 0 0 20
  dll %mds.dcx xdid mds 99 -l 155 95 50 -1
  dll %mds.dcx xdid mds 99 -t 1 +n 0 Project: N/A
  dll %mds.dcx xdid mds 99 -t 2 +  0 Controls: 0
  dll %mds.dcx xdid mds 99 -t 3 +  0 X: N/A
  dll %mds.dcx xdid mds 99 -t 4 +  0 Y: N/A
  %mds.cfg = $qt($scriptdirMDS.db)
  if (!$isfile(%mds.cfg)) write %mds.cfg
  hload -mi mds.cfg %mds.cfg Config
  mds.checkconfig
  mds.recentmenu
  %mds.mapw = $dialog(mds).cw / 10000
  %mds.maph = $dialog(mds).ch / 10000
  dialog -s mds -1 -1 $calc(263*%mds.mapw) $calc(273*%mds.maph)
  $mds.createedit(105,57,90,33,12,5)
  $mds.createedit(106,93,90,33,12,5)
  $mds.createedit(107,129,90,33,12,5)
  $mds.createedit(108,165,90,33,12,5)
  $mds.createedit(110,57,106,33,12,6)
  $mds.createedit(116,165,122,33,12,5)
  $mds.createedit(127,57,207,33,12,12)
  $mds.createedit(128,93,207,33,12,12)
  $mds.createedit(130,57,207,33,12,12)
  $mds.createedit(140,144,159,33,12,6)
  $mds.createedit(208,153,188,33,12,6)
  $mds.createedit(302,66,61,33,12,6)
  $mds.createedit(307,66,95,33,12,5)
  $mds.createedit(308,102,95,33,12,5)
  dll %mds.dcx xdialog mds -c 201 treeview $calc(14 *%mds.mapw) $calc(62 *%mds.maph) $calc(121 *%mds.mapw) $calc(176 *%mds.maph) editlabel fullrow hasbuttons haslines linesatroot showsel
  dialog -ma mds.project. $+ $ticks mds.base
  didtok -ra mds 304 44 notheme,Custom width & height
  if ($hget(mds.cfg,tab.workaround)) {
    did -c mds 304 2
    did -e mds 306-308
  }
  else {
    did -c mds 304 1
    did -b mds 306-308
  }
  did -ra mds 307 $hget(mds.cfg,tab.w)
  did -ra mds 308 $hget(mds.cfg,tab.h)
  tokenize 32 box button check combo edit icon link list radio scroll tab text
  while ($1) {
    did -a mds 310 $1 $hget(mds.cfg,default. $+ $1)
    tokenize 32 $2-
  }
  did -c mds 161
  if (DCX (XPopup) DLL 2.0.1120 * !iswm $dll(%mds.dcx,Version,0)) {
    noop $input($+(Currently loaded DCX.DLL version is potentially incompatible with mIRC Dialog Studio.,$crlf,$crlf,Recommended version:,$crlf,$v2,$crlf,$crlf,Current version:,$crlf,$v1),oia,mIRC Dialog Studio)
  }
}

alias -l mds.createedit {
  dll %mds.dcx xdialog mds -c $1 edit $calc($2 *%mds.mapw) $calc($3 *%mds.maph) $calc($4 *%mds.mapw) $calc($5 *%mds.maph) center tabstop
  dll %mds.dcx xdid mds $1 -q $6
}

on *:dialog:mds:sclick:161:did -c mds 161

on *:dialog:mds:sclick:2:mds.select $did(2) select 0 | did -f mds 2

on *:dialog:mds:sclick:4: {
  if ($did(4) == main dialog) mds.select $did(2) select 0
  elseif ($did(4) == tab 0) mds.select $did(2) select $hget($did(2),tab)
  else mds.select $did(2) select $hget($did(2),$gettok($did(4),2,32))
  did -f mds 4
}

on *:dialog:mds:menu:901: {
  set -l %msg Arrow keys can be used to move controls, and by holding down SHIFT, you can resize them. Holding CTRL down adds a 10x modifier.\n\nSelected controls can be deleted by pressing DEL key.\n\nMultiple controls can be selected by clicking them while holding CTRL down.\n\nMouse wheel and arrow keys can be used to adjust values in the properties dialog. Holding CTRL down adds a 10x modifier, SHIFT 100x.\n\nYou can move controls with mouse by pressing LMB, and resize them by holding down CTRL/SHIFT.\n\nYou can copy and paste selected controls with CTRL+C and CTRL+V respectively.
  noop $input($replacex(%msg,\n,$crlf),oiu,mIRC Dialog Studio)
}

on *:dialog:mds:menu:903: {
  set -l %msg mIRC Dialog Studio v1.1\n\nCoded by: Dazuz @ QuakeNet
  noop $input($replacex(%msg,\n,$crlf),oiu,mIRC Dialog Studio)
}

on *:dialog:mds:sclick:100:mds.select $did(2) select $did(6)

on *:dialog:mds:sclick:200: {
  did -h mds 105-108,110,116,127,128,130,140,302,307,308
  did -v mds 201,208
  mds.loadmenu
}

on *:dialog:mds:sclick:202:.signal xpopup-mds-menu 6

on *:dialog:mds:sclick:203:.signal xpopup-mds-menu 1

on *:dialog:mds:sclick:204:.signal xpopup-mds-menu 2

alias -l mds.deletemenu_1 {
  tokenize 32 $1-
  set -l %id $1
  %mds.path = $puttok(%mds.path,$calc(1+$gettok(%mds.path,-1,32)),-1,32)
  if ($hget(%mds.dname,menu. $+ $1)) {
    %mds.path = %mds.path 1
    tokenize 1 $v1
    mds.deletemenu_2 $*
    %mds.path = $gettok(%mds.path,-2--,32)
  }
  hdel %mds.dname %id
  hdel %mds.dname menu. $+ %id
}

alias -l mds.deletemenu_2 {
  tokenize 32 $1-
  set -l %id $1
  %mds.path = $puttok(%mds.path,$calc(1+$gettok(%mds.path,-1,32)),-1,32)
  if ($hget(%mds.dname,menu. $+ $1)) {
    %mds.path = %mds.path 1
    tokenize 1 $v1
    mds.deletemenu_1 $*
    %mds.path = $gettok(%mds.path,-2--,32)
  }
  hdel %mds.dname %id
  hdel %mds.dname menu. $+ %id
}

on *:dialog:mds:sclick:205:.signal xpopup-mds-menu 3

on *:dialog:mds:sclick:206:.signal xpopup-mds-menu 4

on *:dialog:mds:edit:208:mds.checkmenuid

alias -l mds.checkmenuid {
  if ($dll(%mds.dcx,_xdid,mds 201 selpath)) {
    set -l %dname $did(mds,2)
    set -l %newid $did(mds,208)
    set -l %oldid $gettok($dll(%mds.dcx,_xdid,mds 201 tooltip $v1),2,32)
    if (%newid == %oldid) did -r mds 212
    elseif (%newid == $null) || (%newid !isnum 1-) did -ra mds 212 Invalid ID
    elseif ($hget(%dname,%newid)) did -ra mds 212 $qt(%newid) already exists
    else did -ra mds 212 Press enter to apply
  }
}

on *:dialog:mds:sclick:210,211,213:.signal xpopup-mds-menu 5

alias -l mds.labeledit {
  if ($dll(%mds.dcx,_xdid,mds 201 selpath)) {
    set -l %path $v1
    set -l %tip $dll(%mds.dcx,_xdid,mds 201 tooltip $v1)
    set -l %id $gettok(%tip,2,32)
    set -l %pid $iif($gettok(%tip,4,32) isnum,$v1,root)
    if ($1- == -) && (%pid != root) && (!$hget(%mds.dname,menu. $+ %id)) {
      hadd %mds.dname menu. $+ %pid $puttok($hget(%mds.dname,menu. $+ %pid),$gettok(%tip,2,32) $+(+,$iif($did(mds,210).state,o),$iif($did(mds,211).state,c),q) break,$gettok(%path,-1,32),1)
      did -c mds 213
      .timermds.edit -om 1 0 dll %mds.dcx xdid mds 201 -v %path $chr(9) break
      did -b mds 203,209-211
    }
    else {
      if ($hget(%mds.dname,menu. $+ %id)) || (($1- == break) && ($did(mds,213).state)) did -b mds 203,209-211
      else did -e mds 203,209-211
      hadd %mds.dname menu. $+ %pid $puttok($hget(%mds.dname,menu. $+ %pid),$gettok(%tip,2,32) $+(+,$iif($did(mds,210).state,o),$iif($did(mds,211).state,c),$iif($did(mds,213).state,q)) $1-,$gettok(%path,-1,32),1)
    }
  }
}

on *:signal:xpopup-mds-menu: {
  %mds.dname = $did(mds,2)
  if ($1 == 6) {
    set -l %name Menu $calc(1+$dll(%mds.dcx,_xdid,mds 201 num root))
    set -l %id $mds.getid(%mds.dname)
    hadd %mds.dname %id 1
    hadd %mds.dname menu.root $addtok($hget(%mds.dname,menu.root),%id + %name,1)
    dll %mds.dcx xdid mds 201 -a $+(-1,$chr(9),+e 0 0 0 0 0 0 0 %name,$chr(9),ID: %id)
    dll %mds.dcx xdid mds 201 -c -1
  }
  elseif ($dll(%mds.dcx,_xdid,mds 201 selpath)) {
    %mds.path = $v1
    set -l %tip $dll(%mds.dcx,_xdid,mds 201 tooltip $v1)
    set -l %id $gettok(%tip,2,32)
    set -l %pid $iif($gettok(%tip,4,32) isnum,$v1,root)
    set -l %pos $gettok(%mds.path,-1,32)
    if ($1 == 1) && ($gettok(%tip,2,32)) {
      set -l %pid $v1
      set -l %name Item $calc(1+$dll(%mds.dcx,_xdid,mds 201 num %mds.path))
      set -l %id $mds.getid(%mds.dname)
      hadd %mds.dname %id 1
      hadd %mds.dname menu. $+ %pid $addtok($hget(%mds.dname,menu. $+ %pid),%id + %name,1)
      dll %mds.dcx xdid mds 201 -a $+(%mds.path -1,$chr(9),+e 0 0 0 0 0 0 0 %name,$chr(9),ID: %id Parent: %pid)
      did -b mds 209-211
      did -u mds 210-211
    }
    elseif ($1 == 2) {
      did -b mds 203-213
      did -u mds 210,211,213
      did -r mds 208,212
      dll %mds.dcx xdid mds 201 -d %mds.path
      hdel %mds.dname %id
      hadd %mds.dname menu. $+ %pid $deltok($hget(%mds.dname,menu. $+ %pid),$gettok(%mds.path,-1,32),1)
      tokenize 1 $hget(%mds.dname,menu. $+ %id)
      mds.deletemenu_1 $*
      hdel %mds.dname menu. $+ %id
    }
    elseif ($1 == 3) {
      set -l %tmp $hget(%mds.dname,menu. $+ %pid)
      set -l %token1 $gettok(%tmp,$calc(%pos -1),1)
      set -l %token2 $gettok(%tmp,%pos,1)
      hadd %mds.dname menu. $+ %pid $puttok($puttok(%tmp,%token2,$calc(%pos -1),1),%token1,%pos,1)
      dll %mds.dcx xdid mds 201 -m %mds.path $chr(9) $puttok(%mds.path,$calc(%pos -2),-1,32)
      dll %mds.dcx xdid mds 201 -c $puttok(%mds.path,$calc(%pos -1),-1,32)
      dll %mds.dcx xdid mds 201 -t +a root
    }
    elseif ($1 == 4) {
      set -l %tmp $hget(%mds.dname,menu. $+ %pid)
      set -l %token1 $gettok(%tmp,$calc(1+%pos),1)
      set -l %token2 $gettok(%tmp,%pos,1)
      hadd %mds.dname menu. $+ %pid $puttok($puttok(%tmp,%token2,$calc(1+%pos),1),%token1,%pos,1)
      dll %mds.dcx xdid mds 201 -m %mds.path $chr(9) $puttok(%mds.path,$calc(1+%pos),-1,32)
      dll %mds.dcx xdid mds 201 -c $puttok(%mds.path,$calc(1+%pos),-1,32)
      dll %mds.dcx xdid mds 201 -t +a root
    }
    elseif ($1 == 5) {
      set -l %text $dll(%mds.dcx,_xdid,mds 201 text %mds.path)
      hadd %mds.dname menu. $+ %pid $puttok($hget(%mds.dname,menu. $+ %pid),%id $+(+,$iif($did(mds,210).state,o),$iif($did(mds,211).state,c),$iif($did(mds,213).state,q)) %text,%pos,1)
      if (%pid == root) || ($hget(%mds.dname,menu. $+ %id)) {
        did -b mds 209-211
        did -e mds 203
      }
      elseif (($did(mds,213).state) && (%text == break)) did -b mds 203,209-211
      else did -e mds 203,209-211
    }
  }
}

alias -l mds.menurclick {
  if ($dll(%mds.dcx,_xpopup,mds-menu ismenu)) dll %mds.dcx xpopup mds-menu -d
  dll %mds.dcx xpopup mds-menu -c normal
  dll %mds.dcx xpopup mds-menu -p $rgb(face) $rgb(face) $rgb(face) $rgb(face) $rgb(face) $rgb(shadow) $rgb(3dlight) $rgb(face) $rgb(shadow) $rgb(text) $rgb(text)
  if ($1) {
    dll %mds.dcx xdid mds 201 -c $1-
    set -l %pos $gettok($1-,-1,32)
    set -l %tip $dll(%mds.dcx,_xdid,mds 201 tooltip $1-)
    set -l %id $gettok(%tip,2,32)
    set -l %parent $iif($gettok(%tip,4,32) isnum,$v1,root)
    set -l %item $dll(%mds.dcx,_xdid,mds 201 text $1-)
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 1 0 Add item to $qt(%item)
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 0 0 -
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 2 0 Delete $qt(%item)
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 0 0 -
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 3 0 Move up
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 4 0 Move down
    dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 0 0 -
  }
  dll %mds.dcx xpop mds-menu -a -1 $chr(9) + 6 0 New menu
  dll %mds.dcx xpopup mds-menu -s + $calc($mouse.dx -40) $calc($mouse.dy -15)
  dll %mds.dcx xpopup mds-menu -d
}

alias -l mds.menuselect {
  set -l %dname $did(mds,2)
  if ($1) && ($dll(%mds.dcx,_xdid,mds 201 tooltip $1-)) {
    set -l %id $gettok($v1,2,32)
    set -l %parent $iif($gettok($v1,4,32) isnum,$v1,root)
    set -l %pos $gettok($1-,-1,32)
    did -r mds 212
    did -e mds 203,204,207-213
    if (%pos > 1) did -e mds 205
    else did -b mds 205
    if ($gettok($1-,-2--,32)) set -l %root $v1
    else set -l %root root
    if (%pos < $dll(%mds.dcx,_xdid,mds 201 num %root)) did -e mds 206
    else did -b mds 206
    did -ra mds 208 %id
    set -l %flags $gettok($gettok($hget(%dname,menu. $+ %parent),%pos,1),2,32)
    if (q isin %flags) did -c mds 213
    else did -u mds 213
    if (%parent == root) || ($hget(%dname,menu. $+ %id)) {
      did -e mds 203
      did -b mds 209-211
      did -u mds 210-211
    }
    elseif (($dll(%mds.dcx,_xdid,mds 201 seltext) == break) && (q isin %flags)) {
      did -b mds 203,209-211
      did -u mds 210-211
    }
    else {
      did -e mds 203,209-211
      if (o isin %flags) did -c mds 210
      else did -u mds 210
      if (c isin %flags) did -c mds 211
      else did -u mds 211
    }
  }
  else {
    did -b mds 203-213
    did -u mds 210,211,213
    did -r mds 208,212
  }
}

alias -l mds.loadmenu {
  did -b mds 203-213
  did -u mds 210,211,213
  did -r mds 208,212
  %mds.dname = $did(mds,2)
  dll %mds.dcx xdid mds 201 -r
  tokenize 1 $hget(%mds.dname,menu.root)
  %mds.path = 1
  mds.menu_1 $*
}

alias -l mds.menu_1 {
  tokenize 32 $1-
  dll %mds.dcx xdid mds 201 -a $+(%mds.path,$chr(9),+e 0 0 0 0 0 0 0 $3-,$chr(9),ID: $1 $iif(%mds.parent,Parent: $v1))
  %mds.path = $puttok(%mds.path,$calc(1+$gettok(%mds.path,-1,32)),-1,32)
  if ($hget(%mds.dname,menu. $+ $1)) {
    %mds.path = %mds.path 1
    %mds.parent = $1
    tokenize 1 $v1
    mds.menu_2 $*
    unset %mds.parent
    %mds.path = $gettok(%mds.path,-2--,32)
  }
}

alias -l mds.menu_2 {
  tokenize 32 $1-
  dll %mds.dcx xdid mds 201 -a $+(%mds.path,$chr(9),+e 0 0 0 0 0 0 0 $3-,$chr(9),ID: $1 $iif(%mds.parent,Parent: $v1))
  %mds.path = $puttok(%mds.path,$calc(1+$gettok(%mds.path,-1,32)),-1,32)
  if ($hget(%mds.dname,menu. $+ $1)) {
    %mds.path = %mds.path 1
    %mds.parent = $1
    tokenize 1 $v1
    mds.menu_1 $*
    unset %mds.parent
    %mds.path = $gettok(%mds.path,-2--,32)
  }
}

on *:dialog:mds:sclick:300: {
  did -h mds 105-108,110,116,127,128,130,140,201,208
  did -ra mds 302 $hget($did(2),startid)
  did -v mds 302,307,308
}

on *:dialog:mds:edit:307:mds.changetabw

alias -l mds.changetabw hadd mds.cfg tab.w $iif($did(mds,307) isnum,$v1,70)

on *:dialog:mds:edit:308:mds.changetabh

alias -l mds.changetabh hadd mds.cfg tab.h $iif($did(mds,308) isnum,$v1,25)

on *:dialog:mds:dclick:310: {
  if ($did(310).seltext) {
    tokenize 32 $v1
    if ($input(Enter the changed default values for $upper($1) control in the following format: <WIDTH> <HEIGHT> <STYLES>,equ,mIRC Dialog Studio,$2-)) {
      tokenize 32 $1 $v1
      hadd mds.cfg $1 $iif($2 isnum 1-,$v1,50) $iif($3 isnum 1-,$v1,20) $4-
      did -co mds 310 $did(310).sel $1 $hget(mds.cfg,$1)
    }
  }
}

on *:dialog:mds:sclick:304: {
  if ($did(304).sel == 1) {
    hadd mds.cfg tab.workaround 0
    did -b mds 306-308
  }
  else {
    hadd mds.cfg tab.workaround 1
    did -e mds 306-308
  }
}

on *:dialog:mds:sclick:305:noop $input($+(There's a difference in the size between mIRC and DCX tabs,$chr(44) this option allows you to adjust the DCX tabs used in the mIRC Dialog Studio.,$crlf,$crlf,notheme should have the same size and positioning as the mIRC tabs,$chr(44) but in case it fails,$chr(44) you can use custom width and height.,$crlf,$crlf,Note: this will only take effect in tabs created after changing this option.),oiu,mIRC Dialog Studio)

alias mds.callback {
  if ($2 == mwheel) mds.wheel $1-
  elseif ($2 == keydown) mds.keydown $1-
  elseif ($2 == keyup) mds.keyup $1-
  elseif ($2 == deactivate) mds.deactivate
  elseif ($2-3 == selchange 201) mds.menuselect $4-
  elseif ($2-3 == rclick 201) mds.menurclick $4-
  elseif ($2-3 == labelend 201) mds.labeledit $4-
  elseif ($2 == close) && ($mds.children) return noclose
}

alias -l mds.deactivate {
  unset %mds.shift %mds.ctrl
  .timermds.keydown off
}

on *:dialog:mds:sclick:300:did -ra mds 300 $hget($did(2),startid)

on *:dialog:mds:edit:302:mds.changestartid

alias -l mds.changestartid {
  tokenize 1 $iif($did(mds,302) isnum 1-,$v1,1)
  hadd $did(mds,2) startid $1
  hadd mds.cfg startid $1
}

alias -l mds.wheel {
  if ($istok(105 106 107 108 110 116 128 130 140 208 302 307 308,$3,32)) {
    %mds.dname = $did(mds,2)
    %mds.id = $3
    if (%mds.shift) %mds.modifier = 100
    elseif (%mds.ctrl) %mds.modifier = 10
    else %mds.modifier = 1
    if ($4 == down) mds.wheeldown
    elseif ($4 == up) mds.wheelup
    mds.nextstep
  }
}

alias -l mds.nextstep {
  if (%mds.id == 105) mds.changex
  elseif (%mds.id == 106) mds.changey
  elseif (%mds.id == 107) mds.changew
  elseif (%mds.id == 108) mds.changeh
  elseif (%mds.id == 110) mds.changeid
  elseif (%mds.id == 116) mds.changeiconindex
  elseif ($istok(127 128,%mds.id,32)) mds.changerange
  elseif (%mds.id == 130) mds.changelimit
  elseif (%mds.id == 140) mds.changetabid
  elseif (%mds.id == 208) mds.checkmenuid
  elseif (%mds.id == 302) mds.changestartid
  elseif (%mds.id == 307) mds.changetabw
  elseif (%mds.id == 308) mds.changetabh
}

alias -l mds.wheeldown did -ra mds %mds.id $calc($did(mds,%mds.id)-%mds.modifier)

alias -l mds.wheelup did -ra mds %mds.id $calc($did(mds,%mds.id)+%mds.modifier)

alias -l mds.keydown {
  if ($4 == 38) && ($istok(105 106 107 108 110 116 128 130 140 208 302 307 308,$3,32)) mds.up $3
  elseif ($4 == 40) && ($istok(105 106 107 108 110 116 128 130 140 208 302 307 308,$3,32)) mds.down $3
  elseif ($4 == 16) %mds.shift = 1
  elseif ($4 == 17) %mds.ctrl = 10
}

alias -l mds.up {
  %mds.dname = $did(mds,2)
  %mds.id = $1
  if ($2 > 0) set -l %delay $2 - 100
  elseif ($2 != $null) set -l %delay $2
  else set -l %delay 300
  if (%mds.shift) %mds.modifier = 100
  elseif (%mds.ctrl) %mds.modifier = 10
  else %mds.modifier = 1
  did -ra mds %mds.id $calc($did(mds,%mds.id)+%mds.modifier)
  mds.nextstep
  .timermds.keydown -om 1 %delay mds.up %mds.id %delay
}

alias -l mds.down {
  %mds.dname = $did(mds,2)
  %mds.id = $1
  if ($2 > 0) set -l %delay $2 - 100
  elseif ($2 != $null) set -l %delay $2
  else set -l %delay 300
  if (%mds.shift) %mds.modifier = 100
  elseif (%mds.ctrl) %mds.modifier = 10
  else %mds.modifier = 1
  did -ra mds %mds.id $calc($did(mds,%mds.id)-%mds.modifier)
  mds.nextstep
  .timermds.keydown -om 1 %delay mds.down %mds.id %delay
}

alias -l mds.keyup {
  if ($4 == 16) unset %mds.shift
  elseif ($4 == 17) unset %mds.ctrl
  elseif ($istok(38 40,$4,32)) .timermds.keydown off
}

on *:dialog:mds:sclick:197: {
  %mds.dname = $did(2)
  tokenize 32 %mds.selected
  if ($width(%mds.caption, [ $hget(%mds.dname,font) ] )) %mds.width = $v1 + 4
  else %mds.width = 4
  mds.changeautowidth $*
  dll %mds.dcx xdialog %mds.sdname -j
}

alias -l mds.changeautowidth if ($istok(text link,$hget(%mds.dname,$1 $+ .type),32)) hadd %mds.dname $1 $+ .autowidth $did(mds,197).state

on *:dialog:mds:edit:102: {
  %mds.dname = $did(2)
  %mds.caption = $did(102)
  if ($did(6) == 0) {
    dialog -t %mds.dname %mds.caption
    hadd %mds.dname caption %mds.caption
  }
  else {
    tokenize 32 %mds.selected
    %mds.width = $ceil($calc(($width(%mds.caption, [ $hget(%mds.dname,font) ] )/$hget(%mds.dname,mapw))+3))
    mds.changecaption $*
    did -ra mds 107 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),3,32)
    dll %mds.dcx xdialog %mds.sdname -j
  }
}

alias -l mds.changecaption {
  set -l %type $hget(%mds.dname,$1 $+ .type)
  if (%type == edit) {
    hadd %mds.dname $1 $+ .caption %mds.caption
    dll %mds.dcx xdid %mds.dname $1 -r
    if (%mds.caption) dll %mds.dcx xdid %mds.dname $1 -a $v1
  }
  elseif ($istok(text button check radio box scroll link,%type,32)) {
    hadd %mds.dname $1 $+ .caption %mds.caption
    dll %mds.dcx xdid %mds.dname $1 -t %mds.caption
    if ($hget(%mds.dname,$1 $+ .autowidth)) {
      tokenize 32 $1 $hget(%mds.dname,$1 $+ .xywh)
      hadd %mds.dname $1 $+ .xywh $2-3 %mds.width $5
      dll %mds.dcx xdid %mds.dname $1 -p $round($calc($hget(%mds.dname,mapw)*$2),0) $round($calc($hget(%mds.dname,maph)*$3),0) $round($calc($hget(%mds.dname,mapw)*%mds.width),0) $round($calc($hget(%mds.dname,maph)*$5),0)
    }
  }
}

on *:dialog:mds:sclick:103: {
  %mds.dname = $did(2)
  if ($did(6) == 0) hadd %mds.dname noqt $did(103).state
  else {
    tokenize 32 %mds.selected
    if ($did(mds,103).state) mds.changenoqton $*
    else mds.changenoqtoff $*
  }
}

alias -l mds.changenoqton if ($istok(text edit button check radio box scroll link,$hget(%mds.dname,$1 $+ .type),32)) hadd %mds.dname $1 $+ .noqt 1

alias -l mds.changenoqtoff if ($istok(text edit button check radio box scroll link,$hget(%mds.dname,$1 $+ .type),32)) hdel %mds.dname $1 $+ .noqt

alias -l mds.xywh {
  tokenize 32 $2 $puttok($hget(%mds.dname,$2 $+ .xywh),%mds.v,$1,32)
  hadd %mds.dname $1 $+ .xywh $2-
  dll %mds.dcx xdid %mds.dname $1 -p $round($calc($hget(%mds.dname,mapw)*$2),0) $round($calc($hget(%mds.dname,maph)*$3),0) $round($calc($hget(%mds.dname,mapw)*$4),0) $round($calc($hget(%mds.dname,maph)*$5),0)
}

on *:dialog:mds:edit:105:%mds.dname = $did(2) | mds.changex

alias -l mds.changex {
  if ($did(mds,6) == 0) hadd %mds.dname xywh $puttok($hget(%mds.dname,xywh),$iif($did(mds,105) isnum,$v1,-1),1,32)
  else {
    if ($did(mds,105) isnum) %mds.v = $v1
    else %mds.v = -1
    tokenize 32 %mds.selected
    mds.xywh 1 $*
    dll %mds.dcx xdialog %mds.dname -j
  }
}

on *:dialog:mds:edit:106:%mds.dname = $did(2) | mds.changey

alias -l mds.changey {
  if ($did(mds,6) == 0) hadd %mds.dname xywh $puttok($hget(%mds.dname,xywh),$iif($did(mds,106) isnum,$v1,-1),2,32)
  else {
    if ($did(mds,106) isnum) %mds.v = $v1
    else %mds.v = -1
    tokenize 32 %mds.selected
    mds.xywh 2 $*
    dll %mds.dcx xdialog %mds.dname -j
  }
}

on *:dialog:mds:edit:107:%mds.dname = $did(2) | mds.changew

alias -l mds.changew {
  if ($did(mds,107) isnum) %mds.v = $v1
  else %mds.v = 0
  if ($did(mds,6) == 0) {
    hadd %mds.dname xywh $puttok($hget(%mds.dname,xywh),%mds.v,3,32)
    tokenize 32 $hget(%mds.dname,xywh)
    dialog -s %mds.dname -1 -1 $round($calc($hget(%mds.dname,mapw)*$3),0) $round($calc($hget(%mds.dname,maph)*$4),0)
  }
  else {
    tokenize 32 %mds.selected
    mds.xywh 3 $*
    dll %mds.dcx xdialog %mds.dname -j
  }
}

on *:dialog:mds:edit:108:%mds.dname = $did(2) | mds.changeh

alias -l mds.changeh {
  if ($did(mds,108) isnum) %mds.v = $v1
  else %mds.v = 0
  if ($did(mds,6) == 0) {
    hadd %mds.dname xywh $puttok($hget(%mds.dname,xywh),%mds.v,4,32)
    tokenize 32 $hget(%mds.dname,xywh)
    dialog -s %mds.dname -1 -1 $round($calc($hget(%mds.dname,mapw)*$3),0) $round($calc($hget(%mds.dname,maph)*$4),0)
  }
  else {
    tokenize 32 %mds.selected
    mds.xywh 4 $*
    dll %mds.dcx xdialog %mds.dname -j
  }
}

on *:dialog:mds:edit:110:mds.changeid

alias -l mds.changeid {
  set -l %dname $did(mds,2)
  set -l %newid $did(mds,110)
  set -l %oldid $hget(%dname,$did(mds,6) $+ .id)
  if (%newid == %oldid) did -r mds 111
  elseif (%newid == $null) || (%newid !isnum 1-) did -ra mds 111 Invalid ID
  elseif ($hget(%dname,%newid)) did -ra mds 111 $qt(%newid) already exists
  else did -ra mds 111 Press enter to apply
}

alias -l mds.freeupids if ($hget(%mds.dname,$1 $+ .type) != tab) hdel %mds.dname $hget(%mds.dname,$1 $+ .id)

alias -l mds.redo mds.assignids $*

alias -l mds.assignids {
  if ($hget(%mds.dname,$1 $+ .type) != tab) {
    while ($hget(%mds.dname,%mds.newid)) inc %mds.newid
    hadd %mds.dname %mds.newid $1
    hadd %mds.dname $1 $+ .id %mds.newid
  }
}

on *:dialog:mds:sclick:5: {
  if ($did(110).visible) && ($did(111) == Press enter to apply) {
    %mds.dname = $did(2)
    %mds.id = $did(6)
    %mds.newid = $did(110)
    tokenize 32 %mds.selected
    mds.freeupids $*
    mds.redo %mds.selected
    did -r mds 111
    mds.loadcontrols
    mds.selectcontrol
  }
  elseif ($did(140).visible) && ($did(141) == Press enter to apply) {
    set -l %dname $did(2)
    set -l %did $did(199)
    set -l %newid $did(140)
    hdel %dname $hget(%dname,%did $+ .id)
    hadd %dname %newid %did
    hadd %dname %did $+ .id %newid
    did -r mds 141
  }
  elseif ($did(208).visible) && ($did(212) == Press enter to apply) {
    if ($dll(%mds.dcx,_xdid,mds 201 selpath)) {
      set -l %path $v1
      set -l %dname $did(2)
      set -l %id $did(208)
      set -l %tip $dll(%mds.dcx,_xdid,mds 201 tooltip %path)
      set -l %oldid $gettok(%tip,2,32)
      set -l %parent $iif($gettok(%tip,4,32) isnum,$v1,root)
      set -l %tmp $hget(%dname,menu. $+ %parent)
      set -l %pos $gettok(%path,-1,32)
      hadd %dname menu. $+ %parent $puttok(%tmp,%id $+(+,$iif($did(210).state,o),$iif($did(211).state,c),$iif($did(213).state,q)) $dll(%mds.dcx,_xdid,mds 201 text %path),%pos,1)
      set -l %items $hget(%dname,menu. $+ %oldid)
      hdel %dname menu. $+ %oldid
      if (%items) hadd %dname menu. $+ %id %items
      hdel %dname %oldid
      hadd %dname %id 1
      did -r mds 212
      mds.loadmenu
      dll %mds.dcx xdid mds 201 -c %path
    }
    else beep 1
  }
}

on *:dialog:mds:edit:113:hadd $did(2) name $did(113)

on *:dialog:mds:edit:115: {
  %mds.dname = $did(2)
  %mds.icon = $did(115)
  if ($did(6) == 0) hadd %mds.dname iconfile %mds.icon
  else {
    tokenize 32 %mds.selected
    mds.changeicon $*
  }
}

alias -l mds.changeicon hadd %mds.dname $1 $+ .iconfile %mds.icon

on *:dialog:mds:edit:116:mds.changeiconindex

alias -l mds.changeiconindex {
  %mds.dname = $did(mds,2)
  %mds.index = $iif($did(mds,116) isnum,$v1,0)
  if ($did(6) == 0) hadd %mds.dname iconindex %mds.index
  else {
    tokenize 32 %mds.selected
    mds.changeindex $*
  }
}

alias -l mds.changeindex hadd %mds.dname $1 $+ .iconindex %mds.index

on *:dialog:mds:sclick:118: {
  %mds.dname = $did(2)
  %mds.type = $gettok($did(4),1,32)
  %mds.align = $did(118)
  tokenize 32 %mds.selected
  if ($istok(text edit box,%mds.type,32)) {
    %mds.types = text edit box
    if (%mds.align == left) unset %mds.align
    mds.changealign $*
  }
  elseif ($istok(check radio,%mds.type,32)) {
    %mds.types = check radio
    if (%mds.align == right) unset %mds.align
    mds.changealign $*
  }
  elseif (%mds.type == scroll) {
    if ($istok(right bottom,%mds.align,32)) unset %mds.align
    mds.changescroll $*
  }
}

alias -l mds.changealign {
  if ($istok(%mds.types,$hget(%mds.dname,$1 $+ .type),32)) {
    hadd %mds.dname $1 $+ .align %mds.align
    mds.recreatecontrol %mds.dname $1 $hget(%mds.dname,$1 $+ .pid)
  }
}

alias -l mds.changescroll if ($hget(%mds.dname,$1 $+ .type) == scroll) hadd %mds.dname $1 $+ .align %mds.align

on *:dialog:mds:sclick:120: {
  %mds.dname = $did(2)
  %mds.type = $gettok($did(4),1,32)
  set -l %token $did(120).sel - 1
  set -l %tab $hget(%mds.dname,tab)
  if (%token > 0) %mds.pid = $gettok($hget(%mds.dname,%tab $+ .children),%token,32)
  else %mds.pid = 0
  tokenize 32 %mds.selected
  mds.changetab $*
  dll %mds.dcx xdid %mds.dname %tab -c %token
}

alias -l mds.changetab {
  if ($hget(%mds.dname,$1 $+ .type) != tab) && ($hget(%mds.dname,$1 $+ .pid) != %mds.pid) {
    mds.recreatecontrol %mds.dname $1 %mds.pid
  }
}

alias -l mds.recreatecontrol {
  set -l %dname $1
  set -l %did $2
  set -l %pid $3
  set -l %oldpid $hget(%dname,%did $+ .pid)
  if (%oldpid) dll %mds.dcx xdid %dname $v1 -d %did
  else dll %mds.dcx xdialog %dname -d %did
  hadd %dname %oldpid $+ .children $remtok($hget(%dname,%oldpid $+ .children),%did,0,32)
  hadd %dname %did $+ .pid %pid
  set -l %type $hget(%dname,%did $+ .type)
  if (!%oldpid) && (%pid) {
    tokenize 32 $mds.getmap(%dname,$dll(%mds.dcx,_xdid,%dname %pid dpos)) $hget(%dname,%did $+ .xywh)
    hadd %dname %did $+ .xywh $calc($5 -$1) $calc($6 -$2) $7-
  }
  elseif (%oldpid) && (!%pid) {
    tokenize 32 $mds.getmap(%dname,$dll(%mds.dcx,_xdid,%dname %oldpid dpos)) $hget(%dname,%did $+ .xywh)
    hadd %dname %did $+ .xywh $calc($5 +$1) $calc($6 +$2) $7-
  }
  set -l %xywh $hget(%dname,%did $+ .xywh)
  set -l %styles $mds.getstyles(%type,$hget(%dname,%did $+ .styles) $hget(%dname,%did $+ .align) $hget(%dname,%did $+ .disable) $hget(%dname,%did $+ .hide)) $wildtok($hget(%dname,styles),notheme,1,32) tabstop
  if (%type == tab) {
    ;This should never happen.
    echo 4 -st * mIRC Dialog Studio: something went wrong.
  }
  else {
    if (%pid) {
      dll %mds.dcx xdid %dname %pid -c %did $mds.dcxtype(%type) $mds.getpixels(%dname,%xywh) %styles
      hadd %dname %pid $+ .children $hget(%dname,%pid $+ .children) %did
    }
    else dll %mds.dcx xdialog %dname -c %did $mds.dcxtype(%type) $mds.getpixels(%dname,%xywh) %styles
    if (%type == edit) {
      dll %mds.dcx xdid %dname %did -r
      if ($hget(%dname,%did $+ .caption)) dll %mds.dcx xdid %dname %did -a $v1
    }
    elseif (%type == icon) dll %mds.dcx xdid %dname %did -C +b $rgb(shadow)
    elseif (!$istok(combo list scroll,%type,32)) dll %mds.dcx xdid %dname %did -t $hget(%dname,%did $+ .caption)
    mds.hilightcontrols %dname %mds.selected
  }
}

alias -l mds.getpixels tokenize 32 $hget($1,mapw) $hget($1,maph) $2 | return $round($calc($1 *$3),0) $round($calc($2 *$4),0) $round($calc($1 *$5),0) $round($calc($2 *$6),0) 

alias -l mds.getmap tokenize 32 $hget($1,mapw) $hget($1,maph) $2 | return $round($calc($3 /$1),0) $round($calc($4 /$2),0) $round($calc($5 /$1),0) $round($calc($6 /$2),0) 

on *:dialog:mds:sclick:122: {
  %mds.dname = $did(2)
  %mds.type = $hget(%mds.dname,$gettok($did(6),2,32) $+ .type)
  tokenize 32 %mds.selected
  if ($did(122).state) mds.disableon $*
  else mds.disableoff $*
}

alias -l mds.disableon {
  if ($hget(%mds.dname,$1 $+ .type) != tab) {
    hadd %mds.dname $1 $+ .disable disable
    dll %mds.dcx xdid %mds.dname $1 -b
  }
}

alias -l mds.disableoff {
  if ($hget(%mds.dname,$1 $+ .type) != tab) {
    hdel %mds.dname $1 $+ .disable
    dll %mds.dcx xdid %mds.dname $1 -e
  }
}

on *:dialog:mds:sclick:123: {
  %mds.dname = $did(2)
  %mds.type = $hget(%mds.dname,$gettok($did(6),2,32) $+ .type)
  tokenize 32 %mds.selected
  if ($did(123).state) mds.groupon $*
  else mds.groupoff $*
}

alias -l mds.groupon if ($hget(%mds.dname,$1 $+ .type) != tab) hadd %mds.dname $1 $+ .group group

alias -l mds.groupoff if ($hget(%mds.dname,$1 $+ .type) != tab) hdel %mds.dname $1 $+ .group

on *:dialog:mds:sclick:124: {
  %mds.dname = $did(2)
  %mds.type = $hget(%mds.dname,$gettok($did(6),2,32) $+ .type)
  tokenize 32 %mds.selected
  if ($did(124).state) mds.hideon $*
  else mds.hideoff $*
}

alias -l mds.hideon {
  if ($hget(%mds.dname,$1 $+ .type) != tab) {
    hadd %mds.dname $1 $+ .hide hide
    dll %mds.dcx xdid %mds.dname $1 -R +b 1 250
  }
}

alias -l mds.hideoff {
  if ($hget(%mds.dname,$1 $+ .type) != tab) {
    hdel %mds.dname $1 $+ .hide
    dll %mds.dcx xdid %mds.dname $1 -R +b 0 0
  }
}

on *:dialog:mds:sclick:125: {
  %mds.dname = $did(2)
  %mds.type = $hget(%mds.dname,$gettok($did(6),2,32) $+ .type)
  tokenize 32 %mds.selected
  if ($did(125).state) mds.resulton $*
  else mds.resultoff $*
}

alias -l mds.resulton if ($hget(%mds.dname,$1 $+ .type) != tab) hadd %mds.dname $1 $+ .result result

alias -l mds.resultoff if ($hget(%mds.dname,$1 $+ .type) != tab) hdel %mds.dname $1 $+ .result

on *:dialog:mds:edit:127-128:mds.changerange

alias -l mds.changerange {
  %mds.dname = $did(mds,2)
  %mds.value = $iif($did(mds,127) isnum,$v1,0) $iif($did(mds,128) isnum,$v1,0)
  tokenize 32 %mds.selected
  mds.stylerange $*
}

alias -l mds.stylerange if ($hget(%mds.dname,$1 $+ .type) == scroll) hadd %mds.dname $1 $+ .range %mds.value

on *:dialog:mds:edit:130:mds.changelimit

alias -l mds.changelimit {
  %mds.dname = $did(mds,2)
  %mds.value = $iif($did(mds,130) isnum,$v1,0)
  tokenize 32 %mds.selected
  mds.stylelimit $*
}

alias -l mds.stylelimit if ($istok(edit combo,$hget(%mds.dname,$1 $+ .type),32)) hadd %mds.dname $1 $+ .limit %mds.value

on *:dialog:mds:sclick:131: {
  %mds.dname = $did(2)
  unset %mds.styles
  set -l %x 1
  while ($did(131,%x).csel) {
    %mds.styles = %mds.styles $did(131,$v1)
    inc %x
  }
  if ($did(6) == 0) hadd %mds.dname styles %mds.styles
  else {
    %mds.type = $gettok($did(4),1,32)
    tokenize 32 %mds.selected
    mds.changestyles $*
    if (%mds.type == scroll) mds.checkscroll
  }
}

alias -l mds.checkscroll {
  set -l %did $did(6)
  set -l %align $hget(%mds.dname,%did $+ .align)
  if ($istok($hget(%mds.dname,%did $+ .styles),horizontal,32)) {
    didtok -ra mds 118 32 Top Bottom
    if (%align == top) did -c mds 118 1
    else did -c mds 118 2
  }
  else {
    didtok -ra mds 118 32 Left Right
    if (%align == left) did -c mds 118 1
    else did -c mds 118 2
  }
}

alias -l mds.changestyles {
  if ($hget(%mds.dname,$1 $+ .type) == %mds.type) {
    hadd %mds.dname $1 $+ .styles %mds.styles
    mds.recreatecontrol %mds.dname $1 $hget(%mds.dname,$1 $+ .pid)
  }
}

on *:dialog:mds:sclick:133:mds.opentab

alias -l mds.opentab {
  set -l %dname $did(mds,2)
  set -l %sel $did(mds,133).sel
  set -l %tab $hget(%dname,tab)
  set -l %tid $gettok($hget(%dname,%tab $+ .children),%sel,32)
  did -ra mds 199 %tid
  did -ra mds 137 $hget(%dname,%tid $+ .caption)
  if ($hget(%dname,%tid $+ .noqt)) did -c mds 138
  else did -u mds 138
  did -ra mds 140 $hget(%dname,%tid $+ .id)
  if ($hget(%dname,%tid $+ .childid)) did -c mds 143
  else did -u mds 143
  dll %mds.dcx xdid %dname %tab -c %sel
}

on *:dialog:mds:sclick:134: {
  set -l %dname $did(2)
  set -l %did $hget(%dname,tab)
  hinc %dname id
  set -l %cid $hget(%dname,id)
  set -l %children $hget(%dname,%did $+ .children)
  hadd %dname %did $+ .children %children %cid
  set -l %caption Tab $calc(1+$numtok(%children,32))
  dll %mds.dcx xdid %dname %did -a 0 0 $+(%caption,$chr(9),%cid panel 0 0 0 0)
  hadd %dname %cid $+ .caption %caption
  hadd %dname %cid $+ .pid %did
  set -l %id $mds.getid(%dname)
  hadd %dname %cid $+ .id %id
  hadd %dname %id %cid
  hadd %dname %cid $+ .type panel
  did -a mds 133 $calc(1+$did(133).lines) $+ : %caption
  did -c mds 133 $did(133).lines
  did -e mds 135
  mds.opentab
}

on *:dialog:mds:sclick:135: {
  set -l %dname $did(2)
  set -l %tid $did(199)
  set -l %tab $hget(%dname,tab)
  tokenize 32 $hget(%dname,%tid $+ .children)
  while ($1) {
    dll %mds.dcx xdid %dname %tid -d $1
    hdel %dname $hget(%dname,$1 $+ .id)
    hdel -w %dname $1 $+ .*
    tokenize 32 $2-
  }
  dll %mds.dcx xdid %dname %tab -d $did(133).sel
  hdel %dname $hget(%dname,%tid $+ .id)
  hdel -w %dname %tid $+ .*
  hadd %dname %tab $+ .children $remtok($hget(%dname,%tab $+ .children),%tid,32)
  mds.opentabs
}

on *:dialog:mds:edit:137: {
  set -l %dname $did(2)
  set -l %caption $did(137)
  set -l %sel $did(133).sel
  set -l %tab $hget(%dname,tab)
  hadd %dname $did(199) $+ .caption %caption
  did -co mds 133 %sel %sel $+ : %caption
  dll %mds.dcx xdid %dname %tab -t %sel %caption
}

on *:dialog:mds:sclick:138:hadd $did(2) $did(199) $+ .noqt $did(138).state

on *:dialog:mds:edit:140:mds.changetabid

alias -l mds.changetabid {
  set -l %dname $did(mds,2)
  set -l %newid $did(mds,140)
  set -l %oldid $hget(%dname,$did(mds,199) $+ .id)
  if (%newid == %oldid) did -r mds 141
  elseif (%newid == $null) || (%newid !isnum 1-) did -ra mds 141 Invalid ID
  elseif ($hget(%dname,%newid)) did -ra mds 141 $qt(%newid) already exists
  else did -ra mds 141 Press enter to apply
}

on *:dialog:mds:sclick:142:noop $input($+(When "Use for children" option is checked,$chr(44) any child controls added to the tab will begin with the tab's ID. I.e. if tab's id is 100,$chr(44) the first child control's ID will be 101,$chr(44) and so on.),oiu,mIRC Dialog Studio)

on *:dialog:mds:sclick:143:hadd $did(2) $did(199) $+ .childid $did(143).state

on *:dialog:mds:sclick:151: {
  %mds.dname = $did(2)
  %mds.type = $gettok($did(4),1,32)
  if ($did(151) != center) %mds.align = $v1
  else unset %mds.align
  tokenize 32 %mds.selected
  mds.changeiconalign $*
}

alias -l mds.changeiconalign if ($hget(%mds.dname,$1 $+ .type) == icon) hadd %mds.dname $1 $+ .align %mds.align

on *:dialog:mds:sclick:153: {
  %mds.dname = $did(2)
  %mds.type = $gettok($did(4),1,32)
  if ($did(153) != normal) %mds.size = $v1
  else unset %mds.size
  tokenize 32 %mds.selected
  mds.changeiconsize $*
}

alias -l mds.changeiconsize if ($hget(%mds.dname,$1 $+ .type) == icon) hadd %mds.dname $1 $+ .size %mds.size

on *:dialog:mds:sclick:198: {
  if ($did(198).seltext) {
    did -c mds 100
    set -l %dname $did(2)
    if ($hget(%dname,$gettok($v1,1,58)) > 0) set -l %did $v1
    else set -l %did $hget(%dname,tab)
    %mds.selected = %did $remtok(%mds.selected,%did,0,32)
    %mds.id = %did
    %mds.dname = %dname
    mds.selectcontrol
  }
}

on *:dialog:mds:dclick:198: {
  if ($did(198).seltext) {
    did -c mds 100
    set -l %dname $did(2)
    if ($hget(%dname,$gettok($v1,1,58)) > 0) set -l %did $v1
    else set -l %did $hget(%dname,tab)
    mds.hilightcontrols %dname %did
    %mds.id = %did
    %mds.dname = %dname
    mds.selectcontrol
  }
}

alias -l mds.hilightcontrols {
  set -l %olddname %mds.sdname
  if ($1 == %mds.sdname) {
    set -l %newids $2-
    tokenize 32 %mds.selected
    %mds.selected = %newids
    set -l %unselected
    while ($1) {
      if (!$istok(%mds.selected,$1,32)) %unselected = %unselected $1
      tokenize 32 $2-
    }
  }
  else {
    %mds.sdname = $1
    set -l %unselected %mds.selected
    %mds.selected = $2-
  }
  if ($dialog(%olddname)) && (%unselected) {
    ;dll %mds.dcx xdid %olddname $replacex($v1,$chr(32),$chr(44)) -R +b 0 0
    tokenize 32 $v1
    set -l %border
    while ($1) {
      if (!$istok(combo edit list,$hget(%olddname,$1 $+ .type),32)) %border = $1 %border
      tokenize 32 $2-
    }
    if (%border) dll %mds.dcx xdid %olddname $replacex(%border,$chr(32),$chr(44)) -x +
  }
  if (%mds.selected) {
    ;dll %mds.dcx xdid %mds.sdname $replacex($v1,$chr(32),$chr(44)) -R +b 1 250
    tokenize 32 $v1
    set -l %border
    while ($1) {
      if (!$istok(combo edit list,$hget(%mds.sdname,$1 $+ .type),32)) %border = $1 %border
      tokenize 32 $2-
    }
    if (%border) dll %mds.dcx xdid %mds.sdname $replacex(%border,$chr(32),$chr(44)) -x +b
  }
  dll %mds.dcx xdialog %mds.sdname -j
}

on *:dialog:mds:close:0: {
  hsave -i mds.cfg $qt($scriptdirMDS.db) Config
  if ($com(mds)) .comclose mds
  if ($fopen(mds)) .fclose mds
  set -l %x 1
  while ($dialog(%x)) {
    if (mds.* iswm $v1) dialog -x $v2
    else inc %x
  }
  hfree -w mds.*
  while ($window(@mds.*,1)) window -c $v1
  window -c @MDS
  unset %mds.*
  .timermds.* off
  .remove $qt($scriptdirMDS.tmp)
}

on *:exit: {
  if ($dialog(mds)) {
    unset %mds.*
    hsave -i mds.cfg $qt($scriptdirMDS.db) Config
    .remove $qt($scriptdirMDS.tmp)
  }
}

alias -l mds.children {
  set -l %count 0
  set -l %x 1
  while ($did(mds,2,%x)) {
    inc %count $dll(%mds.dcx,_xdialog,$v1 id 0)
    inc %x
  }
  dec %x
  if ($calc(%count /%x) > 0) && (!$input(Do you really want to exit the mIRC dialog studio?,yqub,mIRC Dialog Studio)) return 1
}

alias -l mds.checkconfig {
  if ($hget(mds.cfg,startid) !isnum 1-) hadd mds.cfg startid 1
  if ($hget(mds.cfg,tab.workaround) !isnum) hadd mds.cfg tab.workaround 1
  if ($hget(mds.cfg,tab.w) !isnum) hadd mds.cfg tab.w 70
  if ($hget(mds.cfg,tab.h) !isnum) hadd mds.cfg tab.h 25
  if (!$hget(mds.cfg,default.box))    hadd mds.cfg default.box    100 100
  if (!$hget(mds.cfg,default.button)) hadd mds.cfg default.button 60  16
  if (!$hget(mds.cfg,default.check))  hadd mds.cfg default.check  100 11
  if (!$hget(mds.cfg,default.combo))  hadd mds.cfg default.combo  100 50   drop
  if (!$hget(mds.cfg,default.edit))   hadd mds.cfg default.edit   100 11
  if (!$hget(mds.cfg,default.icon))   hadd mds.cfg default.icon   100 100
  if (!$hget(mds.cfg,default.link))   hadd mds.cfg default.link   100 10
  if (!$hget(mds.cfg,default.list))   hadd mds.cfg default.list   100 100 size
  if (!$hget(mds.cfg,default.radio))  hadd mds.cfg default.radio  100 11
  if (!$hget(mds.cfg,default.scroll)) hadd mds.cfg default.scroll 12  100
  if (!$hget(mds.cfg,default.tab))    hadd mds.cfg default.tab    200 200
  if (!$hget(mds.cfg,default.text))   hadd mds.cfg default.text   100 10
}

dialog -l mds.base {
  size -1 -1 10000 10000
  icon $scriptdirCube.ico
  option map
}

on *:dialog:mds.project.*:init:0: {
  dll %mds.dcx Mark $dname mds.pcb
  dll %mds.dcx xdialog $dname -c 95432 edit -100 -100 10 10
  tokenize 32 $dll(%mds.dcx,_xdid,$dname 95432 font)
  hfree -w $dname
  hmake $dname
  hadd $dname font $+($4-,$chr(44),-,$3)
  hadd $dname mapw $calc($dialog($dname).cw /10000)
  hadd $dname maph $calc($dialog($dname).ch /10000)
  hadd $dname startid $hget(mds.cfg,startid)
  hadd $dname id 0
  hadd $dname caption New Project
  hadd $dname xywh -1 -1 400 300
  dialog -s $dname -1 -1 $calc($hget($dname,mapw)*400) $calc($hget($dname,maph)*300)
  hadd $dname name new_table
  hadd $dname iconindex 0
  dialog -t $dname New Project
  did -a mds 2 $dname
  set -l %modifier 50 * $did(mds,2).lines
  dialog -s $dname $calc(380+%modifier) $calc(150+%modifier) -1 -1
  if (%mds.dname != $dname) mds.select $dname select 0
}

on *:dialog:mds.project.*:mouse:*:%mds.x = $round($calc($mouse.x /$hget($dname,mapw)),0) | %mds.y = $round($calc($mouse.y /$hget($dname,maph)),0) | %mds.rx = $mouse.x | %mds.ry = $mouse.y

on *:dialog:mds.project.*:close:0: {
  hfree -w $dname
  .timermds.checkprojects -om 1 0 mds.checkprojects
}

alias -l mds.checkprojects {
  if ($dialog(mds)) {
    if ($didwm(mds,2,$dname)) did -d mds 2 $v1
    else {
      did -r mds 2
      set -l %x 1
      while ($hget(%x) != $null) {
        if (mds.project.?* iswm $v1) did -a mds 2 $v2
        inc %x
      }
    }
    if ($did(mds,2).lines) did -c mds 2 1
    else {
      dialog -ma mds.project. $+ $ticks mds.base
      did -c mds 2 1
    }
    mds.select $did(mds,2) select 0
  }
}

alias -l mds.closeproject if (($dll(%mds.dcx,_xdialog,$1 id 0) > 1) || ($hget($1,menu.root))) && (!$input(Do you really want to close the project dialog?,yqub,mIRC Dialog Studio)) return 1

alias mds.pcb {
  if ($2 == mouse) mds.event.mouse $1-
  elseif ($2 == keydown) mds.event.keydown $1-
  elseif ($2 == keyup) mds.event.keyup $1-
  elseif ($2 == lbdown) mds.event.lbdown $1-
  elseif ($2 == lbup) mds.event.lbup $1-
  elseif ($2 == sclick) mds.event.sclick $1-
  elseif ($2 == activate) mds.focus $1-
  elseif ($2 == deactivate) mds.event.deactivate
  elseif ($2 == focus) || ($2-3 == focusout 95432) mds.event.focus $1-
  elseif ($2 == rclick) mds.event.rclick $1-
  elseif ($2 == copy) mds.event.copy $1-
  elseif ($2 == paste) mds.event.paste $1-
  elseif ($2 == close) && ($mds.closeproject($1)) return noclose
}

alias -l mds.event.sclick {
  .timermds.modifyx off
  .timermds.modifyy off
  mds.event.lbup
  unset %mds.hilight
  mds.focus $1-
}

alias -l mds.event.paste {
  if (%mds.copied) {
    %mds.dname = $1
    tokenize 32 %mds.copied
    set -l %source $1
    tokenize 32 $2-
    unset %mds.newids
    mds.createcopy %source $*
    %mds.copied = %mds.dname %mds.newids
    %mds.id = $gettok(%mds.newids,1,32)
    mds.hilightcontrols %mds.dname %mds.newids
    mds.selectcontrol
  }
}

alias -l mds.createcopy {
  set -l %dname $1
  set -l %source $2
  set -l %type $hget(%dname,%source $+ .type)
  if (%type != tab) {
    if (%dname == %mds.dname) set -l %pid $hget(%dname,%source $+ .pid)
    else set -l %pid 0
    hinc %mds.dname id
    set -l %did $hget(%mds.dname,id)
    %mds.newids = %mds.newids %did
    if ($hget(%dname,%pid $+ .childid)) {
      set -l %id $hget(%dname,%pid $+ .id) + 1
      while ($hget(%dname,%id)) inc %id
    }
    else set -l %id $mds.getid(%mds.dname)
    hadd %mds.dname %did $+ .pid %pid
    hadd %mds.dname %id %did
    hadd %mds.dname %did $+ .type %type
    tokenize 32 $hget(%dname,%source $+ .xywh)
    if (%dname == %mds.dname) hadd %mds.dname %did $+ .xywh $calc(10+$1) $calc(10+$2) $3-4
    else hadd %mds.dname %did $+ .xywh $1-
    if ($hget(%dname,%source $+ .range)) hadd %mds.dname %did $+ .range $v1
    if ($hget(%dname,%source $+ .limit)) hadd %mds.dname %did $+ .limit $v1
    if ($hget(%dname,%source $+ .align)) hadd %mds.dname %did $+ .align $v1
    if ($hget(%dname,%source $+ .size)) hadd %mds.dname %did $+ .size $v1
    if ($hget(%dname,%source $+ .styles)) hadd %mds.dname %did $+ .styles $v1
    if ($hget(%dname,%source $+ .disable)) hadd %mds.dname %did $+ .disable $v1
    if ($hget(%dname,%source $+ .group)) hadd %mds.dname %did $+ .group $v1
    if ($hget(%dname,%source $+ .hide)) hadd %mds.dname %did $+ .hide $v1
    if ($hget(%dname,%source $+ .result)) hadd %mds.dname %did $+ .result $v1
    set -l %xywh $hget(%mds.dname,%did $+ .xywh)
    set -l %styles $mds.getstyles(%type,$hget(%dname,%did $+ .styles) $hget(%dname,%did $+ .align) $hget(%dname,%did $+ .disable) $hget(%dname,%did $+ .hide)) $wildtok($hget(%dname,styles),notheme,1,32) tabstop
    hadd %mds.dname %did $+ .id %id
    if (%pid) {
      dll %mds.dcx xdid %mds.dname %pid -c %did $mds.dcxtype(%type) $mds.getpixels(%mds.dname,%xywh) %styles
      hadd %mds.dname %pid $+ .children $hget(%mds.dname,%pid $+ .children) %did
    }
    else dll %mds.dcx xdialog %mds.dname -c %did $mds.dcxtype(%type) $mds.getpixels(%mds.dname,%xywh) %styles
    did -a mds 4 %type %id
    set -l %caption $hget(%dname,%source $+ .caption)
    if (%caption != $null) hadd %mds.dname %did $+ .caption %caption
    if (%type == edit) {
      dll %mds.dcx xdid %mds.dname %did -r
      if (%caption) dll %mds.dcx xdid %mds.dname %did -a $v1
    }
    elseif (%type == icon) {
      hadd %mds.dname %did $+ .iconfile $hget(%dname,%source $+ .iconfile)
      hadd %mds.dname %did $+ .iconindex $iif($hget(%dname,%source $+ .iconindex) isnum 1-,$v1,0)
      dll %mds.dcx xdid %mds.dname %did -C +b $rgb(shadow)
    }
    elseif (!$istok(combo list scroll,%type,32)) dll %mds.dcx xdid %mds.dname %did -t %caption
  }
}

alias -l mds.event.copy {
  if (%mds.selected) && ($1 == %mds.sdname) {
    %mds.copied = $1 %mds.selected
    dll %mds.dcx xdid mds 99 -v 2 $numtok(%mds.selected,32) copied
  }
  else beep 1
}

alias -l mds.event.deactivate {
  unset %mds.shift %mds.ctrl
  .timermds.modifyx off
  .timermds.modifyy off
  mds.event.lbup
}

alias -l mds.event.keydown {
  if ($4 == 37) mds.moveleft $1
  elseif ($4 == 38) mds.moveup $1
  elseif ($4 == 39) mds.moveright $1
  elseif ($4 == 40) mds.movedown $1
  elseif ($4 == 16) %mds.shift = 1
  elseif ($4 == 17) %mds.ctrl = 10
  elseif ($4 == 46) .timermds.delete -om 1 0 mds.deleteselected $1
}

alias -l mds.moveleft {
  %mds.dname = $1
  if ($2 > 0) set -l %delay $2 - 100
  elseif ($2 != $null) set -l %delay $2
  else set -l %delay 300
  if (%mds.ctrl) %mds.modifier = -10
  else %mds.modifier = -1
  if (%mds.selected) {
    tokenize 32 $v1
    if (%mds.shift) {
      mds.w $*
      did -ra mds 107 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),3,32)
    }
    else {
      mds.x $*
      did -ra mds 105 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),1,32)
    }
  }
  else {
    tokenize 32 $hget(%mds.dname,xywh)
    if (%mds.shift) {
      hadd %mds.dname xywh $1-2 $calc($3 %mds.modifier) $4
      tokenize 32 $hget(%mds.dname,xywh)
      dialog -s %mds.dname -1 -1 $round($calc($hget(%mds.dname,mapw)*$3),0) $round($calc($hget(%mds.dname,maph)*$4),0)
      did -ra mds 107 $3
    }
    else {
      hadd %mds.dname xywh $calc($1 %mds.modifier) $2-4
      did -ra mds 105 $gettok($hget(%mds.dname,xywh),1,32)
    }
  }
  .timermds.modifyx -om 1 %delay mds.moveleft %mds.dname %delay
}

alias -l mds.moveright {
  %mds.dname = $1
  if ($2 > 0) set -l %delay $2 - 100
  elseif ($2 != $null) set -l %delay $2
  else set -l %delay 300
  if (%mds.ctrl) %mds.modifier = +10
  else %mds.modifier = +1
  if (%mds.selected) {
    tokenize 32 $v1
    if (%mds.shift) {
      mds.w $*
      did -ra mds 107 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),3,32)
    }
    else {
      mds.x $*
      did -ra mds 105 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),1,32)
    }
  }
  else {
    tokenize 32 $hget(%mds.dname,xywh)
    if (%mds.shift) {
      hadd %mds.dname xywh $1-2 $calc($3 %mds.modifier) $4
      tokenize 32 $hget(%mds.dname,xywh)
      dialog -s %mds.dname -1 -1 $round($calc($hget(%mds.dname,mapw)*$3),0) $round($calc($hget(%mds.dname,maph)*$4),0)
      did -ra mds 107 $3
    }
    else {
      hadd %mds.dname xywh $calc($1 %mds.modifier) $2-4
      did -ra mds 105 $gettok($hget(%mds.dname,xywh),1,32)
    }
  }
  .timermds.modifyx -om 1 %delay mds.moveright %mds.dname %delay
}

alias -l mds.w {
  tokenize 32 $1 $hget(%mds.dname,$1 $+ .xywh)
  hadd %mds.dname $1 $+ .xywh $2-3 $calc($4 %mds.modifier) $5
  dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$hget(%mds.dname,$1 $+ .xywh))
}

alias -l mds.x {
  tokenize 32 $1 $hget(%mds.dname,$1 $+ .xywh)
  hadd %mds.dname $1 $+ .xywh $calc($2 %mds.modifier) $3-5
  dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$hget(%mds.dname,$1 $+ .xywh))
}

alias -l mds.moveup {
  %mds.dname = $1
  if ($2 > 0) set -l %delay $2 - 100
  elseif ($2 != $null) set -l %delay $2
  else set -l %delay 300
  if (%mds.ctrl) %mds.modifier = -10
  else %mds.modifier = -1
  if (%mds.selected) {
    tokenize 32 $v1
    if (%mds.shift) {
      mds.h $*
      did -ra mds 108 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),4,32)
    }
    else {
      mds.y $*
      did -ra mds 106 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),2,32)
    }
  }
  else {
    tokenize 32 $hget(%mds.dname,xywh)
    if (%mds.shift) {
      hadd %mds.dname xywh $1-3 $calc($4 %mds.modifier)
      tokenize 32 $hget(%mds.dname,xywh)
      dialog -s %mds.dname -1 -1 $round($calc($hget(%mds.dname,mapw)*$3),0) $round($calc($hget(%mds.dname,maph)*$4),0)
      did -ra mds 108 $4
    }
    else {
      hadd %mds.dname xywh $1 $calc($2 %mds.modifier) $3-
      did -ra mds 106 $gettok($hget(%mds.dname,xywh),2,32)
    }
  }
  .timermds.modifyy -om 1 %delay mds.moveup %mds.dname %delay
}

alias -l mds.movedown {
  %mds.dname = $1
  if ($2 > 0) set -l %delay $2 - 100
  elseif ($2 != $null) set -l %delay $2
  else set -l %delay 300
  if (%mds.ctrl) %mds.modifier = +10
  else %mds.modifier = +1
  if (%mds.selected) {
    tokenize 32 $v1
    if (%mds.shift) {
      mds.h $*
      did -ra mds 108 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),4,32)
    }
    else {
      mds.y $*
      did -ra mds 106 $gettok($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .xywh),2,32)
    }
  }
  else {
    tokenize 32 $hget(%mds.dname,xywh)
    if (%mds.shift) {
      hadd %mds.dname xywh $1-3 $calc($4 %mds.modifier)
      tokenize 32 $hget(%mds.dname,xywh)
      dialog -s %mds.dname -1 -1 $round($calc($hget(%mds.dname,mapw)*$3),0) $round($calc($hget(%mds.dname,maph)*$4),0)
      did -ra mds 108 $4
    }
    else {
      hadd %mds.dname xywh $1 $calc($2 %mds.modifier) $3-
      did -ra mds 106 $gettok($hget(%mds.dname,xywh),2,32)
    }
  }
  .timermds.modifyy -om 1 %delay mds.movedown %mds.dname %delay
}

alias -l mds.h {
  tokenize 32 $1 $hget(%mds.dname,$1 $+ .xywh)
  hadd %mds.dname $1 $+ .xywh $2-4 $calc($5 %mds.modifier)
  dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$hget(%mds.dname,$1 $+ .xywh))
}

alias -l mds.y {
  tokenize 32 $1 $hget(%mds.dname,$1 $+ .xywh)
  hadd %mds.dname $1 $+ .xywh $2 $calc($3 %mds.modifier) $4-5
  dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$hget(%mds.dname,$1 $+ .xywh))
}

alias -l mds.event.keyup {
  if ($4 isnum 37-40) dll %mds.dcx xdialog %mds.dname -j
  if ($istok(37 39,$4,32)) .timermds.modifyx off
  elseif ($istok(38 40,$4,32)) .timermds.modifyy off
  elseif ($4 == 16) unset %mds.shift
  elseif ($4 == 17) unset %mds.ctrl
}

alias -l mds.focus dll %mds.dcx xdid $1 95432 -F

alias -l mds.event.focus if ($3 != 95432) dll %mds.dcx xdid $1 95432 -F

alias -l mds.event.mouse {
  if (!$timer(mds.statusbar)) .timermds.statusbar -om 1 50 mds.updatestatusbar $1-
  if (%mds.move) mds.movecontrols $v1
  elseif (%mds.hilight) mds.hilight
}

alias -l mds.hilight {
  tokenize 32 $v1 %mds.x %mds.y
  if ($2 > $4) var %x = $4,%w $2 - $4
  else var %x = $2,%w $4 - $2
  if ($3 > $5) var %y = $5,%h $3 - $5
  else var %y = $3,%h $5 - $3
  dll %mds.dcx xdid $1 95431 -p $mds.getpixels($1,%x %y %w %h)
}

alias -l mds.movecontrols {
  set -l %x %mds.x - %mds.sx
  set -l %y %mds.y - %mds.sy
  if (%mds.shift) && (%mds.ctrl) mds.sxywh $* %x %y
  elseif (%mds.shift) mds.wxywh $* %x -
  elseif (%mds.ctrl) mds.hxywh $* - %y
  else {
    mds.mxywh $* %x %y
    dll %mds.dcx xdialog %mds.dname -j
  }
}

alias -l mds.mxywh tokenize 32 $1-3 $hget(%mds.dname,$1 $+ .xywh) | dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$calc($4 +$2) $calc($5 +$3) $6-)

alias -l mds.sxywh tokenize 32 $1-3 $hget(%mds.dname,$1 $+ .xywh) | dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$4-5 $calc($6 +$2) $calc($7 +$3))

alias -l mds.wxywh tokenize 32 $1-3 $hget(%mds.dname,$1 $+ .xywh) | dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$4-5 $calc($6 +$2) $7)

alias -l mds.hxywh tokenize 32 $1-3 $hget(%mds.dname,$1 $+ .xywh) | dll %mds.dcx xdid %mds.dname $1 -p $mds.getpixels(%mds.dname,$4-6 $calc($7 +$3))

alias -l mds.updatestatusbar {
  dll %mds.dcx xdid mds 99 -v 1 $1
  dll %mds.dcx xdid mds 99 -v 3 X: %mds.x
  dll %mds.dcx xdid mds 99 -v 4 Y: %mds.y
  if (%mds.move) mds.updatexywh
}

alias -l mds.updatexywh {
  tokenize 32 $dll(%mds.dcx,_xdid,%mds.dname $gettok(%mds.selected,1,32) pos)
  did -ra mds 105 $round($calc($1 /$hget(%mds.dname,mapw)),0)
  did -ra mds 106 $round($calc($2 /$hget(%mds.dname,maph)),0)
}

alias -l mds.event.lbdown {
  %mds.dname = $1
  mds.getcontrols
  if ($gettok($sorttok(%mds.controls,1,nr),1,1)) tokenize 32 %mds.dname select $gettok($v1,3,32)
  else tokenize 32 %mds.dname select 0
  mds.select $1-
  if ($dll(%mds.dcx,_xdialog,%mds.dname isid 95431)) .timermds.delete -om 1 0 dll %mds.dcx xdialog %mds.dname -d 95431
  unset %mds.hilight
  if (%mds.move) mds.event.lbup
  if (!%mds.stop) {
    if (%mds.selected) {
      if ($hget(%mds.dname,$gettok(%mds.selected,1,32) $+ .type) != list) {
        %mds.sx = %mds.x
        %mds.sy = %mds.y
        if ($istok(%mds.selected,$hget(%mds.dname,tab),32)) {
          tokenize 32 %mds.selected
          while ($1) {
            if (!$hget(%mds.dname,$1 $+ .pid)) %mds.move = %mds.move $1
            tokenize 32 $2-
          }
        }
        else %mds.move = %mds.selected
      }
    }
    else .timermds.hilight -om 1 1 mds.starthilight
  }
}

alias -l mds.starthilight {
  %mds.hilight = %mds.dname %mds.x %mds.y
  dll %mds.dcx xdialog %mds.dname -c 95431 box %mds.rx %mds.ry 0 0 transparent
}

alias -l mds.changexywh hadd %mds.dname $1 $+ .xywh $mds.getmap(%mds.dname,$dll(%mds.dcx,_xdid,%mds.dname $1 pos))

alias -l mds.event.lbup {
  if (%mds.move) {
    tokenize 32 $v1
    mds.changexywh $*
  }
  if (%mds.hilight) .timermds.delete -om 1 0 mds.deletehilight $v1
  unset %mds.move %mds.sx %mds.sy %mds.hilight
}

alias -l mds.deletehilight {
  set -l %dname $1
  tokenize 32 $dll(%mds.dcx,_xdid,$1 95431 pos)
  %mds.poly = $replacex($1 $2 $calc($1 +$3) $2 $calc($1 +$3) $calc($2 +$4) $1 $calc($2 +$4),$chr(32),$chr(44))
  noop $hfind(%dname,*?.xywh,0,w,mds.checkhilight $gettok($1,1,46) $dll(%mds.dcx,_xdid,%dname $gettok($1,1,46) dpos))
  dll %mds.dcx xdialog %dname -j
  dll %mds.dcx xdialog %dname -d 95431
  tokenize 32 $sorttok(%mds.selected,32,n)
  mds.hilightcontrols %dname $1-
  %mds.id = $1
  if (%mds.id isnum 1-) mds.selectcontrol
  else mds.selectmaindialog
}

alias -l mds.checkhilight if ($onpoly(4,4, [ %mds.poly ] ,$2,$3,$calc($2 +$4),$3,$calc($2 +$4),$calc($3 +$5),$2,$calc($3 +$5))) %mds.selected = $1 %mds.selected

alias -l mds.event.rclick {
  mds.event.lbup

  if ($dll(%mds.dcx,_xpopup,mds-tools ismenu)) dll %mds.dcx xpopup mds-tools -d
  dll %mds.dcx xpopup mds-tools -c normal
  dll %mds.dcx xpopup mds-tools -p $rgb(face) $rgb(face) $rgb(face) $rgb(face) $rgb(face) $rgb(shadow) $rgb(3dlight) $rgb(face) $rgb(shadow) $rgb(text) $rgb(text)

  %mds.dname = $1
  unset %mds.pid
  mds.getcontrols
  if (%mds.pid) {
    tokenize 32 $dll(%mds.dcx,_xdid,%mds.dname %mds.pid mouse)
    %mds.x = $round($calc($1 /$hget(%mds.dname,mapw)),0)
    %mds.y = $round($calc($2 /$hget(%mds.dname,maph)),0)
  }

  if ($numtok(%mds.controls,1) == 0) mds.nomenuentries
  elseif ($v1 == 1) mds.onemenuentry
  else mds.multiplemenuentries
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 0 0 -

  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 50 0 Box
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 51 0 Button
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 52 0 Check
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 53 0 Combo
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 54 0 Edit
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 55 0 Icon
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 56 0 Link
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 57 0 List
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 58 0 Radio
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 59 0 Scroll
  if ($hget(%mds.dname,tab)) dll %mds.dcx xpop mds-tools -a -1 $chr(9) +g 60 0 Tab (already exists)
  else dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 60 0 Tab
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 61 0 Text
  dll %mds.dcx xpopup mds-tools -s + $calc($mouse.dx -40) $calc($mouse.dy -15)
  dll %mds.dcx xpopup mds-tools -d
}

alias -l mds.inrect tokenize 32 $1 | if ($inrect(%mds.x,%mds.y,$1,$2,$3,$4)) return 1

alias -l mds.getcontrols {
  unset %mds.controls
  if ($hget(%mds.dname,tab)) && ($mds.inrect($hget(%mds.dname,$v1 $+ .xywh))) {
    %mds.pid = $dll(%mds.dcx,_xdid,%mds.dname $hget(%mds.dname,tab) childid $dll(%mds.dcx,_xdid,%mds.dname $hget(%mds.dname,tab) sel))
    tokenize 32 $dll(%mds.dcx,_xdialog,%mds.dname mouse)
    %mds.x = $round($calc($1 /$hget(%mds.dname,mapw)),0)
    %mds.y = $round($calc($2 /$hget(%mds.dname,maph)),0)
    noop $hfind(%mds.dname,*?.xywh,0,w,mds.checktabcontrol $gettok($1,1,46) $dll(%mds.dcx,_xdid,%mds.dname $gettok($1,1,46) dpos))
  }
  else {
    %mds.pid = 0
    noop $hfind(%mds.dname,*?.xywh,0,w,mds.checkcontrol $1 $dll(%mds.dcx,_xdid,%mds.dname $gettok($1,1,46) dpos))
  }
}

alias -l mds.checktabcontrol if ($inrect(%mds.rx,%mds.ry,$2,$3,$4,$5)) && ($istok(0 %mds.pid,$hget(%mds.dname,$1 $+ .pid),32)) mds.addcontrolentry $1

alias -l mds.checkcontrol if ($inrect(%mds.rx,%mds.ry,$2,$3,$4,$5)) mds.addcontrolentry $gettok($1,1,46)

alias -l mds.addcontrolentry %mds.controls = $addtok(%mds.controls,$+($hget(%mds.dname,$1 $+ .pid),.,$1) $hget(%mds.dname,$1 $+ .type) $1,1)

alias -l mds.nomenuentries dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 101 0 Select "Main Dialog"

alias -l mds.onemenuentry {
  tokenize 32 %mds.controls
  tokenize 32 $qt($2 $hget(%mds.dname,$3 $+ .id))
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 101 0 Select $1-
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 1001 0 Move $1-
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 10001 0 Delete $1-
}

alias -l mds.multiplemenuentries {
  %mds.menuindex = 101
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 0 0 Select...
  dll %mds.dcx xpop mds-tools -c 1
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 0 0 Move...
  dll %mds.dcx xpop mds-tools -c 2
  dll %mds.dcx xpop mds-tools -a -1 $chr(9) + 0 0 Delete...
  dll %mds.dcx xpop mds-tools -c 3
  %mds.controls = $sorttok(%mds.controls,1,nr)
  tokenize 1 %mds.controls
  mds.addentry $*
}

alias -l mds.addentry {
  tokenize 32 $qt($2 $hget(%mds.dname,$3 $+ .id))
  dll %mds.dcx xpop mds-tools -a 1 -1 $chr(9) + %mds.menuindex 0 Select $1-
  dll %mds.dcx xpop mds-tools -a 2 -1 $chr(9) + $calc(900+%mds.menuindex) 0 Move $1-
  dll %mds.dcx xpop mds-tools -a 3 -1 $chr(9) + $calc(9900+%mds.menuindex) 0 Delete $1-
  inc %mds.menuindex
}

on *:signal:xpopup-mds-tools: {
  if ($1 isnum 50-61) mds.createcontrol %mds.dname $replacex($1,50,box,51,button,52,check,53,combo,54,edit,55,icon,56,link,57,list,58,radio,59,scroll,60,tab,61,text) %mds.pid
  elseif ($1 isnum 101-999) mds.select %mds.dname select $gettok($gettok(%mds.controls,$calc($1 -100),1),3,32)
  elseif ($1 isnum 1001-9999) {
    set -l %did $gettok($gettok(%mds.controls,$calc($1 -1000),1),3,32)
    mds.select %mds.dname select %did
    mds.hilightcontrols %mds.dname %did
    %mds.move = %did
    tokenize 32 $dll(%mds.dcx,_xdialog,%mds.dname mouse)
    %mds.x = $round($calc($1 /$hget(%mds.dname,mapw)),0)
    %mds.y = $round($calc($2 /$hget(%mds.dname,maph)),0)
    %mds.sx = %mds.x
    %mds.sy = %mds.y
  }
  elseif ($1 isnum 10001-99999) .timermds.delete -om 1 0 mds.menudelete %mds.dname $gettok($gettok(%mds.controls,$calc($1 -10000),1),3,32)
  else set -u1 %mds.stop 1
}

alias -l mds.menudelete {
  mds.deletecontrol $1-
  mds.loadcontrols
  mds.select $1 select 0
}

alias -l mds.deleteselected {
  set -l %dname $1
  tokenize 32 %mds.selected
  mds.hilightcontrols %dname
  while ($1) {
    if ($hget(%dname,$1 $+ .type)) mds.deletecontrol %dname $1
    tokenize 32 $2-
  }
  unset %mds.move %mds.selected
  mds.loadcontrols
  mds.select %dname select 0
}

alias -l mds.deletecontrol {
  if ($hget($1,$2 $+ .type) == tab) {
    set -l %dname $1
    set -l %tid $2
    set -l %tabs $hget(%dname,%tid $+ .children)
    set -l %x 1
    while ($gettok(%tabs,%x,32)) {
      set -l %did $v1
      tokenize 32 $hget(%dname,%did $+ .children)
      while ($1) {
        mds.deletecontrol_ %dname $1
        tokenize 32 $2-
      }
      hdel %dname $hget(%dname,%did $+ .id)
      hdel -w %dname %did $+ .*
      inc %x
    }
    hdel %dname tab
    mds.deletecontrol_ %dname %tid
  }
  else mds.deletecontrol_ $1-
}

alias -l mds.deletecontrol_ {
  if ($hget($1,$2 $+ .pid)) {
    dll %mds.dcx xdid $1 $v1 -d $2
    hadd %mds.dname $v1 $+ .children $remtok($hget(%mds.dname,$v1 $+ .children),$2,0,32)
  }
  else dll %mds.dcx xdialog $1 -d $2
  hdel $1 $hget($1,$2 $+ .id)
  hdel -w $1 $2 $+ .*
}

alias -l mds.dcxtype return $replacex($1,combo,comboex,icon,image)

alias -l mds.getid {
  set -l %id $hget($1,startid)
  while ($hget($1,%id)) inc %id
  return %id
}

alias -l mds.createcontrol {
  set -l %dname $1
  set -l %type $2
  set -l %pid $3
  hinc %dname id
  set -l %did $hget(%dname,id)
  if ($hget(%dname,%pid $+ .childid)) {
    set -l %id $hget(%dname,%pid $+ .id) + 1
    while ($hget(%dname,%id)) inc %id
  }
  else set -l %id $mds.getid(%dname)
  hadd %dname %did $+ .pid %pid
  hadd %dname %id %did
  hadd %dname %did $+ .type %type
  if ($hget(mds.cfg,default. $+ %type)) {
    tokenize 32 $v1
    hadd %dname %did $+ .xywh %mds.x %mds.y $1-2
    if ($3) hadd %dname %did $+ .styles $3-
  }
  else hadd %dname %did $+ .xywh %mds.x %mds.y 50 50
  set -l %xywh $hget(%dname,%did $+ .xywh)
  set -l %styles $mds.getstyles(%type,$hget(%dname,%did $+ .styles)) $wildtok($hget(%dname,styles),notheme,1,32) tabstop
  if (%type == tab) {
    hadd %dname tab %did
    hadd %dname %did $+ .id 0
    if ($hget(mds.cfg,tab.workaround)) {
      dll %mds.dcx xdialog %dname -c %did tab $mds.getpixels(%dname,%xywh) %styles fixedwidth
      dll %mds.dcx xdid %dname %did -m $hget(mds.cfg,tab.w) $hget(mds.cfg,tab.h)
    }
    else dll %mds.dcx xdialog %dname -c %did tab $mds.getpixels(%dname,%xywh) %styles notheme
    hinc %dname id
    set -l %cid $hget(%dname,id)
    hadd %dname %did $+ .children $hget(%dname,%did $+ .children) %cid
    dll %mds.dcx xdid %dname %did -a 0 0 $+(Tab 1,$chr(9),%cid panel 0 0 0 0)
    hadd %dname %cid $+ .caption Tab 1
    hadd %dname %cid $+ .pid %did
    hadd %dname %cid $+ .id %id
    hadd %dname %cid $+ .type panel
    mds.select %dname select %did
  }
  else {
    hadd %dname %did $+ .id %id
    if (%pid) {
      dll %mds.dcx xdid %dname %pid -c %did $mds.dcxtype(%type) $mds.getpixels(%dname,%xywh) %styles
      hadd %dname %pid $+ .children $hget(%dname,%pid $+ .children) %did
    }
    else dll %mds.dcx xdialog %dname -c %did $mds.dcxtype(%type) $mds.getpixels(%dname,%xywh) %styles
    did -a mds 4 %type %id
    if (%type == box) {
      dll %mds.dcx xdid %dname %did -t Group Box
      hadd %dname %did $+ .caption Group Box
    }
    elseif (%type == button) {
      dll %mds.dcx xdid %dname %did -t Button
      hadd %dname %did $+ .caption Button
    }
    elseif (%type == edit) {
      dll %mds.dcx xdid %dname %did -r
      dll %mds.dcx xdid %dname %did -a Edit box
      hadd %dname %did $+ .caption Edit box
    }
    elseif (%type == text) {
      dll %mds.dcx xdid %dname %did -t Text Label
      hadd %dname %did $+ .caption Text Label
    }
    elseif (%type == link) {
      dll %mds.dcx xdid %dname %did -t Link
      hadd %dname %did $+ .caption Link
    }
    elseif (%type == check) {
      dll %mds.dcx xdid %dname %did -t Check box
      hadd %dname %did $+ .caption Check box
    }
    elseif (%type == radio) {
      dll %mds.dcx xdid %dname %did -t Radio button
      hadd %dname %did $+ .caption Radio button
    }
    elseif (%type == scroll) dll %mds.dcx xdid %dname %did -r 0 0
    elseif (%type == icon) {
      hadd %dname %did $+ .iconindex 0
      dll %mds.dcx xdid %dname %did -C +b $rgb(shadow)
    }
    mds.select %dname select %did
  }
}

alias -l mds.getstyles {
  set -l %styles
  if ($1 == box) {
    %styles = %styles noformat transparent
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,right,32)) %styles = %styles right
    if ($istok($2,center,32)) %styles = %styles center
  }
  elseif ($1 == button) {
    %styles = %styles noformat
    ;if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
  }
  elseif ($1 == check) {
    %styles = %styles transparent
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,3state,32)) %styles = %styles 3state
    if ($istok($2,push,32)) %styles = %styles pushlike
    if ($istok($2,left,32)) %styles = %styles right
  }
  elseif ($1 == combo) {
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,drop,32)) {
      if ($istok($2,edit,32)) %styles = %styles dropedit
      else %styles = %styles dropdown
    }
    else %styles = %styles simple
    if ($istok($2,hsbar,32)) %styles = %styles hscroll
  }
  elseif ($1 == edit) {
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,autohs,32)) %styles = %styles autohs
    if ($istok($2,autovs,32)) %styles = %styles autovs
    if ($istok($2,center,32)) %styles = %styles center
    if ($istok($2,right,32)) %styles = %styles right
    if ($istok($2,hsbar,32)) %styles = %styles hsbar
    if ($istok($2,multi,32)) %styles = %styles multi
    if ($istok($2,pass,32)) %styles = %styles password
    if ($istok($2,read,32)) %styles = %styles readonly
    if ($istok($2,vsbar,32)) %styles = %styles vsbar
  }
  elseif ($1 == icon) {
    %styles = %styles
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
  }
  elseif ($1 == link) {
    %styles = %styles noformat transparent
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
  }
  elseif ($1 == list) {
    %styles = %styles noformat nointegral
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,vsbar,32)) %styles = %styles vsbar
    if ($istok($2,hsbar,32)) %styles = %styles hsbar
  }
  elseif ($1 == radio) {
    %styles = %styles transparent
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,left,32)) %styles = %styles right
    if ($istok($2,push,32)) %styles = %styles pushlike
  }
  elseif ($1 == scroll) {
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if (!$istok($2,horizontal,32)) %styles = %styles vertical
  }
  elseif ($1 == tab) {
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
  }
  elseif ($1 == text) {
    set -l %styles noformat transparent
    if ($istok($2,hide,32)) %styles = %styles alpha
    if ($istok($2,disable,32)) %styles = %styles disabled
    if ($istok($2,nowrap,32)) %styles = %styles nowrap
    if ($istok($2,right,32)) %styles = %styles right
    if ($istok($2,center,32)) %styles = %styles center
  }
  return %styles
}

alias -l mds.select {
  %mds.dname = $1
  %mds.id = $3
  did -r mds 111,141
  if ($didwm(mds,2,%mds.dname)) did -c mds 2 $v1
  else did -ca mds 2 %mds.dname
  if (%mds.dname != %mds.lastdname) mds.loadcontrols
  if (%mds.dname != %mds.sdname) mds.hilightcontrols %mds.dname $iif(%mds.id isnum 1-,$v1)
  elseif (%mds.id isnum 1-) {
    if (%mds.ctrl) {
      if ($istok(%mds.selected,%mds.id,32)) {
        tokenize 32 $remtok(%mds.selected,%mds.id,0,32)
        mds.hilightcontrols %mds.dname $1-
        %mds.id = $1
      }
      else mds.hilightcontrols %mds.dname %mds.id %mds.selected
    }
    elseif (!$istok(%mds.selected,%mds.id,32)) mds.hilightcontrols %mds.dname %mds.id
  }
  else %mds.id = 0
  did -c mds 100
  if (%mds.id isnum 1-) mds.selectcontrol
  else mds.selectmaindialog
}

alias -l mds.selectcontrol {
  dll %mds.dcx xdid mds 99 -v 2 $numtok(%mds.selected,32) selected
  set -l %type $hget(%mds.dname,%mds.id $+ .type)
  set -l %id $hget(%mds.dname,%mds.id $+ .id)
  did -ra mds 6 %mds.id
  if ($didwm(mds,4,%type %id)) did -c mds 4 $v1
  else did -ca mds 4 %type %id
  tokenize 32 $hget(%mds.dname,%mds.id $+ .xywh)
  did -ra mds 105 $1
  did -ra mds 106 $2
  did -ra mds 107 $3
  did -ra mds 108 $4
  did -re mds 198
  tokenize 32 %mds.selected
  mds.addcontrol $*
  did -c mds 198 1
  if (%type == box) mds.selectbox
  elseif (%type == button) mds.selectbutton
  elseif (%type == check) mds.selectcheck
  elseif (%type == combo) mds.selectcombo
  elseif (%type == edit) mds.selectedit
  elseif (%type == icon) mds.selecticon
  elseif (%type == link) mds.selectlink
  elseif (%type == list) mds.selectlist
  elseif (%type == radio) mds.selectradio
  elseif (%type == scroll) mds.selectscroll
  elseif (%type == tab) mds.selecttab
  elseif (%type == text) mds.selecttext
  else did -h mds 101-143,150-153
}

alias -l mds.addcontrol did -a mds 198 $hget(%mds.dname,$1 $+ .id) $+ : $hget(%mds.dname,$1 $+ .type)

alias -l mds.selectbox {
  did -h mds 112-113,114-116,126-128,129-130,131,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 101-103,104-108,109-111,117-118,119-120,121-125
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  didtok -ra mds 118 32 Left Center Right
  set -l %align $hget(%mds.dname,%mds.id $+ .align)
  if (%align == center) did -c mds 118 2
  elseif (%align == right) did -c mds 118 3
  else did -c mds 118 1
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectbutton {
  did -h mds 112-113,114-116,117-118,126-128,129-130,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 101-103,104-108,109-111,119-120,121-125,131
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),default ok cancel flat multi)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectcheck {
  did -h mds 112-113,114-116,126-128,129-130,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 101-103,104-108,109-111,117-118,119-120,121-125,131
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  didtok -ra mds 118 32 Left Right
  if ($hget(%mds.dname,%mds.id $+ .align) == left) did -c mds 118 1
  else did -c mds 118 2
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),push 3state)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectcombo {
  did -h mds 101-103,112-113,114-116,117-118,126-128,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 104-108,109-111,119-120,121-125,129-130,131
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  if ($hget(%mds.dname,%mds.id $+ .limit)) did -ra mds 130 $v1
  else did -ra mds 130 0
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),sort edit drop size vsbar hsbar)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectedit {
  did -h mds 112-113,114-116,126-128,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 101-103,104-108,109-111,117-118,119-120,121-125,129-130,131
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  didtok -ra mds 118 32 Left Center Right
  set -l %align $hget(%mds.dname,%mds.id $+ .align)
  if (%align == center) did -c mds 118 2
  elseif (%align == right) did -c mds 118 3
  else did -c mds 118 1
  if ($hget(%mds.dname,%mds.id $+ .limit)) did -ra mds 130 $v1
  else did -ra mds 130 0
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),multi pass read return hsbar vsbar autohs autovs rich)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selecticon {
  did -h mds 101-103,112-113,117-118,126-128,129-130,132-143,160,161,201,208,302,307,308,197
  did -v mds 104-108,109-111,114-116,119-120,121-125,131,150-153
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  did -ra mds 115 $hget(%mds.dname,%mds.id $+ .iconfile)
  did -ra mds 116 $hget(%mds.dname,%mds.id $+ .iconindex)
  didtok -ra mds 151 32 Top Left Center Right Bottom
  set -l %align $hget(%mds.dname,%mds.id $+ .align)
  if (%align == top) did -c mds 151 1
  elseif (%align == left) did -c mds 151 2
  elseif (%align == right) did -c mds 151 4
  elseif (%align == bottom) did -c mds 151 5
  else did -c mds 151 3
  didtok -ra mds 153 32 Normal Small Large Actual
  set -l %size $hget(%mds.dname,%mds.id $+ .size)
  if (%size == small) did -c mds 153 2
  elseif (%size == large) did -c mds 153 3
  elseif (%size == actual) did -c mds 153 4
  else did -c mds 153 1
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),noborder)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectlink {
  did -h mds 112-113,114-116,117-118,126-128,129-130,131,132-143,150-153,160,161,201,208,302,307,308
  did -v mds 101-103,104-108,109-111,119-120,121-125,197
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  if ($hget(%mds.dname,%mds.id $+ .autowidth)) did -c mds 197 $v1
  else did -u mds 197
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectlist {
  did -h mds 101-103,112-113,114-116,117-118,126-128,129-130,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 104-108,109-111,119-120,121-125,131
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),sort extsel multsel size vsbar hsbar check radio)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectradio {
  did -h mds 112-113,114-116,126-128,129-130,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 101-103,104-108,109-111,117-118,119-120,121-125,131
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  didtok -ra mds 118 32 Left Right
  if ($hget(%mds.dname,%mds.id $+ .align) == left) did -c mds 118 1
  else did -c mds 118 1
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),push)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selectscroll {
  did -h mds 112-113,114-116,129-130,132-143,150-153,160,161,201,208,302,307,308,197
  did -v mds 101-103,104-108,109-111,117-118,119-120,121-125,126-128,131
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  if ($hget(%mds.dname,%mds.id $+ .range)) {
    tokenize 32 $v1
    did -ra mds 127 $1
    did -ra mds 128 $2
  }
  else did -ra mds 127,128 0
  set -l %styles $hget(%mds.dname,%mds.id $+ .styles)
  set -l %align $hget(%mds.dname,%mds.id $+ .align)
  if ($istok(%styles,horizontal,32)) {
    didtok -ra mds 118 32 Top Bottom
    if (%align == top) did -c mds 118 1
    else did -c mds 118 2
  }
  else {
    didtok -ra mds 118 32 Left Right
    if (%align == left) did -c mds 118 1
    else did -c mds 118 2
  }
  $mds.checkstyles(%styles,horizontal)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.selecttext {
  did -h mds 112-113,114-116,126-128,129-130,132-143,150-153,160,161,201,208,302,307,308
  did -v mds 101-103,104-108,109-111,117-118,119-120,121-125,131,197
  did -ra mds 102 $hget(%mds.dname,%mds.id $+ .caption)
  if ($hget(%mds.dname,%mds.id $+ .noqt)) did -c mds 103 $v1
  else did -u mds 103
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  didtok -ra mds 118 32 Left Center Right
  set -l %align $hget(%mds.dname,%mds.id $+ .align)
  if (%align == center) did -c mds 118 2
  elseif (%align == right) did -c mds 118 3
  else did -c mds 118 1
  if ($hget(%mds.dname,%mds.id $+ .autowidth)) did -c mds 197 $v1
  else did -u mds 197
  $mds.checkstyles($hget(%mds.dname,%mds.id $+ .styles),nowrap)
  mds.checkoptions
  mds.checktabs
}

alias -l mds.checkoptions {
  if ($hget(%mds.dname,%mds.id $+ .disable)) did -c mds 122
  else did -u mds 122
  if ($hget(%mds.dname,%mds.id $+ .group)) did -c mds 123
  else did -u mds 123
  if ($hget(%mds.dname,%mds.id $+ .hide)) did -c mds 124
  else did -u mds 124
  if ($hget(%mds.dname,%mds.id $+ .result)) did -c mds 125
  else did -u mds 125
}

alias -l mds.checkstyles {
  didtok -ra mds 131 32 $2
  tokenize 32 $1
  while ($1) {
    if ($didwm(mds,131,$1)) did -s mds 131 $v1
    tokenize 32 $2-
  }
}

alias -l mds.checktabs {
  if ($hget(%mds.dname,tab)) {
    set -l %tab $v1
    did -e mds 119
    %mds.pid = 0
    set -l %x 1
    if ($hget(%mds.dname,%mds.id $+ .pid)) {
      did -rae mds 120 None
      set -l %pid $v1
      while ($dll(%mds.dcx,_xdid,%mds.dname %tab childid %x)) {
        did -a mds 120 %x $+ : $hget(%mds.dname,$v1 $+ .caption)
        if ($v1 == %pid) tokenize 32 $v1 %x
        inc %x
      }
      %mds.pid = $1
      did -c mds 120 $calc(1+$2)
      dll %mds.dcx xdid %mds.dname %tab -c $2
    }
    else {
      did -crae mds 120 None
      while ($dll(%mds.dcx,_xdid,%mds.dname %tab childid %x)) {
        did -a mds 120 %x $+ : $hget(%mds.dname,$v1 $+ .caption)
        inc %x
      }
    }
  }
  else {
    did -rb mds 120
    did -b mds 119
  }
}

alias -l mds.selecttab {
  did -h mds 101-103,109-111,112-113,114-116,117-118,119-120,121-125,126-128,131,150-153,160,161,201,208,302,307,308,197
  did -v mds 104-108,132-143
  if ($hget(%mds.dname,%mds.id $+ .pid)) {
    set -l %pid $v1
    set -l %tab $hget(%mds.dname,tab)
    set -l %x 1
    while ($dll(%mds.dcx,_xdid,%mds.dname %tab childid %x)) {
      if ($v1 == %pid) break
      inc %x
    }
    dll %mds.dcx xdid %mds.dname %tab -c %x
  }
  did -ra mds 110 $hget(%mds.dname,%mds.id $+ .id)
  .timermds.opentabs -om 1 0 mds.opentabs
}

alias -l mds.opentabs {
  did -r mds 133
  set -l %tab $hget(%mds.dname,tab)
  set -l %children $hget(%mds.dname,%tab $+ .children)
  tokenize 32 %children
  set -l %x 1
  while ($1) {
    did -a mds 133 %x $+ : $hget(%mds.dname,$1 $+ .caption)
    inc %x
    tokenize 32 $2-
  }
  set -l %sel $iif($dll(%mds.dcx,_xdid,%mds.dname %tab sel) isnum 1-,$v1,0)
  did -c mds 133 %sel
  set -l %tid $gettok(%children,%sel,32)
  did -ra mds 199 %tid
  set -l %tid $gettok($hget(%mds.dname,%tab $+ .children),%sel,32)
  did -ra mds 137 $hget(%mds.dname,%tid $+ .caption)
  if ($hget(%mds.dname,%tid $+ .noqt)) did -c mds 138
  else did -u mds 138
  did -ra mds 140 $hget(%mds.dname,%tid $+ .id)
  if ($hget(%mds.dname,%tid $+ .childid)) did -c mds 143
  else did -u mds 143
  if ($did(mds,133).lines > 1) did -e mds 135
  else did -b mds 135
}

alias -l mds.loadcontrols {
  did -r mds 4
  noop $hfind(%mds.dname,*?.type,0,w,mds.ac $gettok($1,1,46) $hget(%mds.dname,$1))
  filter -ciotux 2 32 mds 4 mds 4 panel *
  did -i mds 4 1 Main Dialog
  %mds.lastdname = %mds.dname
}

alias -l mds.ac did -a mds 4 $2 $hget(%mds.dname,$1 $+ .id)

alias -l mds.selectmaindialog {
  did -h mds 109-111,117-118,119-120,121-125,126-128,129-130,132-143,150-153,201,208,302,307,308,197
  did -v mds 101-103,104-108,112-113,114-116,131,160,161

  mds.hilightcontrols %mds.dname
  did -rb mds 198

  dll %mds.dcx xdid mds 99 -v 2 $calc($dll(%mds.dcx,_xdialog,%mds.dname id 0)-1) controls

  did -c mds 4 1
  did -ra mds 6 0

  did -ra mds 102 $hget(%mds.dname,caption)
  if ($hget(%mds.dname,noqt)) did -c mds 103
  else did -u mds 103

  tokenize 32 $hget(%mds.dname,xywh)
  did -ra mds 105 $1
  did -ra mds 106 $2
  did -ra mds 107 $3
  did -ra mds 108 $4

  did -ra mds 113 $hget(%mds.dname,name)

  did -ra mds 115 $hget(%mds.dname,iconfile)
  did -ra mds 116 $hget(%mds.dname,iconindex)

  didtok -ra mds 131 32 notheme disable local
  tokenize 32 $hget(%mds.dname,styles)
  while ($1) {
    if ($didwm(mds,131,$1)) did -s mds 131 $v1
    tokenize 32 $2-
  }
}

on *:dialog:mds:menu:401:dialog -ma mds.project. $+ $ticks mds.base

on *:dialog:mds:menu:402:dialog -c %mds.dname

on *:dialog:mds:menu:406:dialog -c mds

on *:dialog:mds:menu:501:noop $dialog(mds.import,mds.import,-4)

dialog -l mds.import {
  title "mIRC Dialog Studio - Import dialog"
  icon $scriptdirCube.ico
  size -1 -1 360 335
  option map
  edit "", 1, 5 4 350 300, autohs autovs hsbar multi rich vsbar return
  button "Close", 2, 285 310 70 20, ok
  button "Import", 3, 211 310 70 20, default
}

on *:dialog:mds.import:init:0: {
  did -f mds.import 1
  if ($com(mds)) .comclose mds
  .comopen mds WScript.Shell
  if ($comerr) {
    set -l %x 1
    set -l %t $cb(0)
    while (%x <= %t) { did -i mds.import 1 %x $cb(%x) | inc %x }
  }
  else .comclose mds $com(mds,SendKeys,1,bstr,^v)
}

on *:dialog:mds.import:sclick:3: {
  window -c @mds.import
  window -h @mds.import
  filter -iw mds.import 1 @mds.import
  dialog -c mds.import
  if ($did(mds,2).lines == 1) && ($dll(%mds.dcx,_xdialog,$did(mds,2) id 0) == 1) && (!$hget($did(mds,2),menu.root)) %mds.close = $did(mds,2)
  else unset %mds.close
  dialog -v mds
  unset %mds.dialog
  filter -wk @mds.import mds.line
  window -c @mds.import
  if (%mds.dialog) mds.endofdialog
}

on *:dialog:mds:menu:502: {
  unset %mds.dialog
  if ($mds.checkdir($nofile($gettok($hget(mds.cfg,recent),1,1)))) set -l %dir $v1
  else set -l %dir $mircdir
  set -l %file $sfile(%dir *.mrc,Import from...,Import)
  if (!$sfstate) && ($isfile(%file)) {
    hadd mds.cfg recent $instok($remtok($hget(mds.cfg,recent),%file,0,1),%file,1,1)
    mds.recentmenu
    filter -fk $qt(%file) mds.line
    if (%mds.dialog) mds.endofdialog
  }
}

alias -l mds.recentmenu {
  set -l %x 1001
  while ($did(mds,%x).isid) {
    did -d mds 404 %x
    inc %x
  }
  %mds.i = 1001
  tokenize 1 $hget(mds.cfg,recent)
  if ($0) {
    mds.addrecent $*
    did -e mds 404
  }
  else did -b mds 404
}

alias -l mds.addrecent did -a mds 404 %mds.i $nopath($1-)

on *:dialog:mds:menu:1001-: {
  set -l %file $gettok($hget(mds.cfg,recent),$calc($did -1000),1)
  hadd mds.cfg recent $instok($remtok($hget(mds.cfg,recent),%file,0,1),%file,1,1)
  filter -fk $qt(%file) mds.line
}

alias -l mds.endofdialog {
  if (%mds.close) {
    set -l %xy $dialog(%mds.close).x $dialog(%mds.close).y
    dialog -c %mds.close
  }
  else set -l %xy -1 -1
  unset %mds.dialog %mds.close
  tokenize 32 $hget(%mds.dname,xywh)
  tokenize 32 $iif($1 isnum,$1,-1) $iif($2 isnum,$2,-1) $iif($3 isnum,$3,0) $iif($4 isnum,$4,0)
  if (%mds.dtype == map) tokenize 32 $1-2 $iif($3 == -1,-1,$round($calc($3 *$hget(%mds.dname,mapw)),0)) $iif($4 == -1,-1,$round($calc($4 *$hget(%mds.dname,maph)),0))
  elseif (%mds.dtype == dbu) tokenize 32 $1-2 $iif($3 == -1,-1,$round($calc($3 *$dbuw),0)) $iif($4 == -1,-1,$round($calc($4 *$dbuh),0))
  dialog -s %mds.dname %xy $3-
  hadd %mds.dname xywh $1-2 $round($calc($3 /$hget(%mds.dname,mapw)),0) $round($calc($4 /$hget(%mds.dname,maph)),0)
  mds.select %mds.dname select 0
}

alias -l mds.line {
  tokenize 32 $1-
  if ($1 == dialog) {
    %mds.dname = mds.project. $+ $ticks
    dialog -v mds
    dialog -ma %mds.dname mds.base
    if ($2 == -l) {
      hadd %mds.dname styles local
      hadd %mds.dname name $3
    }
    else hadd %mds.dname name $2
    %mds.dialog = 1
  }
  elseif (%mds.dialog) {
    if ($1 == $chr(125)) mds.endofdialog
    elseif ($1 == title) {
      if ("* iswm $2) hadd %mds.dname caption $gettok($2-,1,34)
      else {
        hadd %mds.dname caption $2
        hadd %mds.dname noqt 1
      }
      dialog -t %mds.dname $hget(%mds.dname,caption)
    }
    elseif ($1 == size) hadd %mds.dname xywh $iif($2 isnum,$2,-1) $iif($3 isnum,$3,-1) $iif($4 isnum 0-,$4,50) $iif($5 isnum 0-,$5,50)
    elseif ($1 == option) {
      if ($istok($2-,dbu,32)) %mds.dtype = dbu
      elseif ($istok($2-,map,32)) %mds.dtype = map
      else %mds.dtype = pixels
      hadd %mds.dname styles $remtok($remtok($remtok($2- $hget(%mds.dname,styles),pixels,0,32),dbu,0,32),map,0,32)
    }
    elseif ($2 != $null) {
      if ($1 == tab) mds.parsetab $1-
      elseif ($istok(text edit button check radio box scroll link,$1,32)) mds.parsetypea $1-
      elseif ($istok(list combo,$1,32)) mds.parsetypeb $1-
      elseif ($1 == icon) mds.parseicon $1-
      elseif ($1 == menu) mds.parsemenu $1-
      elseif ($1 == item) mds.parseitem $1-
    }
  }
}

alias -l mds.parsemenu {
  bset -tc & 1 $2-
  set -l %caption $mds.gcaption
  set -l %id $mds.gid
  set -l %pid $mds.gpid
  if (%pid) && (%id) {
    hadd %mds.dname %id 1
    hadd %mds.dname menu. $+ %pid $addtok($hget(%mds.dname,menu. $+ %pid),%id + $+ $iif(%mds.noqt,q) %caption,1)
  }
  elseif (%id) {
    hadd %mds.dname %id 1
    hadd %mds.dname menu.root $addtok($hget(%mds.dname,menu.root),%id + $+ $iif(%mds.noqt,q) %caption,1)
  }
}

alias -l mds.parseitem {
  bset -tc & 1 $2-
  set -l %caption $mds.gcaption
  set -l %id $mds.gid
  set -l %pid $mds.gpid
  if ($bvar(&,0)) set -l %styles $bvar(&,1-).text
  if (%pid) && (%id) {
    hadd %mds.dname %id 1
    hadd %mds.dname menu. $+ %pid $addtok($hget(%mds.dname,menu. $+ %pid),%id $+(+,$iif($istok(%styles,ok,32),o),$iif($istok(%styles,cancel,32),c),$iif(%mds.noqt,q)) %caption,1)
  }
}

alias -l mds.parsetab {
  bset -tc & 1 $2-
  set -l %caption $mds.gcaption
  if ($mds.gid) {
    set -l %id $v1
    if ($hget(%mds.dname,tab)) {
      set -l %tid $v1
      hinc %mds.dname id
      set -l %did $hget(%mds.dname,id)
      hadd %mds.dname %did $+ .pid %tid
      hadd %mds.dname %did $+ .id %id
      if (%caption) hadd %mds.dname %did $+ .caption $v1
      hadd %mds.dname %did $+ .type panel
      hadd %mds.dname %id %did
      hadd %mds.dname %tid $+ .children $addtok($hget(%mds.dname,%tid $+ .children),%did,32)
      if (%mds.noqt) hadd %mds.dname %did $+ .noqt 1
      dll %mds.dcx xdid %mds.dname %tid -a 0 0 $+(%caption,$chr(9),%did panel 0 0 0 0)
    }
    elseif ($mds.gxywh) {
      set -l %xywh $v1
      hinc %mds.dname id
      set -l %did $hget(%mds.dname,id)
      hadd %mds.dname tab %did
      hadd %mds.dname %did $+ .xywh %xywh
      hadd %mds.dname %did $+ .id 0
      hadd %mds.dname %did $+ .pid 0
      hadd %mds.dname %did $+ .type tab
      if ($hget(mds.cfg,tab.workaround)) {
        dll %mds.dcx xdialog %mds.dname -c %did tab $mds.getpixels(%mds.dname,%xywh) fixedwidth
        dll %mds.dcx xdid %mds.dname %did -m $hget(mds.cfg,tab.w) $hget(mds.cfg,tab.h)
      }
      else dll %mds.dcx xdialog %mds.dname -c %did tab $mds.getpixels(%mds.dname,%xywh) notheme
      hinc %mds.dname id
      set -l %cid $hget(%mds.dname,id)
      hadd %mds.dname %did $+ .children %cid
      hadd %mds.dname %id %cid
      if (%mds.noqt) hadd %mds.dname %did $+ .noqt 1
      dll %mds.dcx xdid %mds.dname %did -a 0 0 $+(%caption,$chr(9),%cid panel 0 0 0 0)
      hadd %mds.dname %cid $+ .caption %caption
      hadd %mds.dname %cid $+ .pid %did
      hadd %mds.dname %cid $+ .id %id
      hadd %mds.dname %cid $+ .type panel
      tokenize 32 $dll(%mds.dcx,_xdid,%mds.dname %cid dpos)
      %mds.offsetx = $round($calc($1 /$hget(%mds.dname,mapw)),0)
      %mds.offsety = $round($calc($2 /$hget(%mds.dname,maph)),0)
    }
  }
}

alias -l mds.parsetypea {
  bset -tc & 1 $2-
  set -l %caption $mds.gcaption
  if ($mds.gid) {
    set -l %id $v1
    if ($mds.gxywh) {
      set -l %xywh $v1
      if ($bvar(&,0)) set -l %styles $bvar(&,1-).text
      else set -l %styles
      hinc %mds.dname id
      set -l %did $hget(%mds.dname,id)
      hadd %mds.dname %id %did
      hadd %mds.dname %did $+ .xywh %xywh
      hadd %mds.dname %did $+ .id %id
      if (%mds.noqt) hadd %mds.dname %did $+ .noqt 1
      if (%caption) hadd %mds.dname %did $+ .caption $v1
      hadd %mds.dname %did $+ .type $1
      hadd %mds.dname %did $+ .pid 0
      if ($istok(%styles,top,32)) hadd %mds.dname %did $+ .align top
      elseif ($istok(%styles,bottom,32)) hadd %mds.dname %did $+ .align bottom
      elseif ($istok(%styles,left,32)) hadd %mds.dname %did $+ .align left
      elseif ($istok(%styles,right,32)) hadd %mds.dname %did $+ .align right
      elseif ($istok(%styles,center,32)) hadd %mds.dname %did $+ .align center
      if ($istok(%styles,disable,32)) hadd %mds.dname %did $+ .disable disable
      if ($istok(%styles,group,32)) hadd %mds.dname %did $+ .group group
      if ($istok(%styles,result,32)) hadd %mds.dname %did $+ .result result
      if ($istok(%styles,hide,32)) hadd %mds.dname %did $+ .hide hide
      if ($findtok(%styles,tab,1,32)) {
        set -l %pos $v1
        %styles = $deltok(%styles,%pos,32)
        if ($gettok(%styles,%pos,32) isnum 1-) {
          set -l %tid $hget(%mds.dname,$v1)
          %styles = $deltok(%styles,%pos,32)
          if (%tid) && ($hget(%mds.dname,tab)) {
            hadd %mds.dname %did $+ .pid %tid
            hadd %mds.dname %tid $+ .children $addtok($hget(%mds.dname,%tid $+ .children),%did,32)
            tokenize 32 %xywh
            hadd %mds.dname %did $+ .xywh $calc($1 -%mds.offsetx) $calc($2 -%mds.offsety) $3-4
          }
        }
      }
      if ($findtok(%styles,limit,1,32)) {
        set -l %pos $v1
        %styles = $deltok(%styles,%pos,32)
        if ($gettok(%styles,%pos,32) isnum 1-) {
          hadd %mds.dname %did $+ .limit $v1
          %styles = $deltok(%styles,%pos,32)
        }
      }
      if ($findtok(%styles,range,1,32)) {
        set -l %pos $v1
        %styles = $deltok(%styles,%pos,32)
        if ($gettok(%styles,%pos,32) isnum) {
          set -l %n1 $v1
          %styles = $deltok(%styles,%pos,32)
          if ($gettok(%styles,%pos,32) isnum) {
            hadd %mds.dname %did $+ .range %n1 $v1
            %styles = $deltok(%styles,%pos,32)
          }
          else hadd %mds.dname %did $+ .range %n1 0
        }
      }
      tokenize 32 top left center bottom right disable group result hide
      while ($1) {
        %styles = $remtok(%styles,$1,0,32)
        tokenize 32 $2-
      }
      if (%styles) hadd %mds.dname %did $+ .styles $v1
      mds.importcontrol %did
    }
  }
}

alias -l mds.parsetypeb {
  bset -tc & 1 $2-
  if ($mds.gid) {
    set -l %id $v1
    if ($mds.gxywh) {
      set -l %xywh $v1
      if ($bvar(&,0)) set -l %styles $bvar(&,1-).text
      else set -l %styles
      hinc %mds.dname id
      set -l %did $hget(%mds.dname,id)
      hadd %mds.dname %id %did
      hadd %mds.dname %did $+ .xywh %xywh
      hadd %mds.dname %did $+ .id %id
      hadd %mds.dname %did $+ .type $1
      hadd %mds.dname %did $+ .pid 0
      if ($istok(%styles,disable,32)) hadd %mds.dname %did $+ .disable disable
      if ($istok(%styles,group,32)) hadd %mds.dname %did $+ .group group
      if ($istok(%styles,result,32)) hadd %mds.dname %did $+ .result result
      if ($istok(%styles,hide,32)) hadd %mds.dname %did $+ .hide hide
      if ($istok(%styles,top,32)) hadd %mds.dname %did $+ .align top
      elseif ($istok(%styles,bottom,32)) hadd %mds.dname %did $+ .align bottom
      elseif ($istok(%styles,center,32)) hadd %mds.dname %did $+ .align center
      elseif ($istok(%styles,left,32)) hadd %mds.dname %did $+ .align left
      elseif ($istok(%styles,right,32)) hadd %mds.dname %did $+ .align right
      if ($findtok(%styles,tab,1,32)) {
        set -l %pos $v1
        %styles = $deltok(%styles,%pos,32)
        if ($gettok(%styles,%pos,32) isnum 1-) {
          set -l %tid $hget(%mds.dname,$v1)
          %styles = $deltok(%styles,%pos,32)
          if (%tid) && ($hget(%mds.dname,tab)) {
            hadd %mds.dname %did $+ .pid %tid
            hadd %mds.dname %tid $+ .children $addtok($hget(%mds.dname,%tid $+ .children),%did,32)
            tokenize 32 %xywh
            hadd %mds.dname %did $+ .xywh $calc($1 -%mds.offsetx) $calc($2 -%mds.offsety) $3-4
          }
        }
      }
      tokenize 32 top left center bottom right disable group result hide
      while ($1) {
        %styles = $remtok(%styles,$1,0,32)
        tokenize 32 $2-
      }
      hadd %mds.dname %did $+ .styles %styles
      mds.importcontrol %did
    }
  }
}

alias -l mds.parseicon {
  bset -tc & 1 $2-
  if ($mds.gid) {
    set -l %id $v1
    if ($mds.gxywh) {
      set -l %xywh $v1
      hinc %mds.dname id
      set -l %did $hget(%mds.dname,id)
      hadd %mds.dname %id %did
      hadd %mds.dname %did $+ .xywh %xywh
      hadd %mds.dname %did $+ .id %id
      hadd %mds.dname %did $+ .type $1
      hadd %mds.dname %did $+ .pid 0
      set -l %styles
      if ($bvar(&,0)) {
        set -l %tmp $bvar(&,1-).text
        set -l %index
        set -l %icon
        set -l %x 1
        while ($gettok(%tmp,%x,44)) {
          tokenize 1 $v1
          if (%index !isnum) && ($1 isnum 0-) %index = $1
          elseif (%icon == $null) && ($mds.checkiconstyle($1)) %icon = $noqt($1)
          elseif (!%styles) %styles = $1
          inc %x
        }
        if (%index != $null) hadd %mds.dname %did $+ .iconindex $v1
        if (%icon != $null) hadd %mds.dname %did $+ .iconfile $v1
      }
      if ($istok(%styles,disable,32)) hadd %mds.dname %did $+ .disable disable
      if ($istok(%styles,group,32)) hadd %mds.dname %did $+ .group group
      if ($istok(%styles,result,32)) hadd %mds.dname %did $+ .result result
      if ($istok(%styles,hide,32)) hadd %mds.dname %did $+ .hide hide
      if ($istok(%styles,top,32)) hadd %mds.dname %did $+ .align top
      elseif ($istok(%styles,bottom,32)) hadd %mds.dname %did $+ .align bottom
      elseif ($istok(%styles,left,32)) hadd %mds.dname %did $+ .align left
      elseif ($istok(%styles,right,32)) hadd %mds.dname %did $+ .align right
      if ($istok(%styles,small,32)) hadd %mds.dname %did $+ .size small
      elseif ($istok(%styles,large,32)) hadd %mds.dname %did $+ .size large
      elseif ($istok(%styles,actual,32)) hadd %mds.dname %did $+ .size actual
      if ($findtok(%styles,tab,1,32)) {
        set -l %pos $v1
        %styles = $deltok(%styles,%pos,32)
        if ($gettok(%styles,%pos,32) isnum 1-) {
          set -l %tid $hget(%mds.dname,$v1)
          %styles = $deltok(%styles,%pos,32)
          if (%tid) && ($hget(%mds.dname,tab)) {
            hadd %mds.dname %did $+ .pid %tid
            hadd %mds.dname %tid $+ .children $addtok($hget(%mds.dname,%tid $+ .children),%did,32)
            tokenize 32 %xywh
            hadd %mds.dname %did $+ .xywh $calc($1 -%mds.offsetx) $calc($2 -%mds.offsety) $3-4
          }
        }
      }
      tokenize 32 top left bottom right disable group result hide
      while ($1) {
        %styles = $remtok(%styles,$1,0,32)
        tokenize 32 $2-
      }
      hadd %mds.dname %did $+ .styles %styles
      mds.importcontrol %did
    }
  }
  elseif ($noqt($gettok($2,1,44)) != $null) {
    hadd %mds.dname iconfile $v1
    hadd %mds.dname iconindex $iif($gettok($2,2,44) isnum 0-,$v1,0)
  }
}

alias -l mds.checkiconstyle {
  set -l %input $1
  tokenize 32 noborder top left bottom right small large actual disable group result hide
  while ($1) {
    %input = $remtok(%input,$1,0,32)
    tokenize 32 $2-
  }
  tokenize 32 %input
  if ($1 == tab) && ($2 isnum 1-) && ($3 == $null) return 0
  else return 1
}

alias -l mds.gtok {
  if ($bvar(&,0)) {
    if ($bfind(&,1,44)) {
      tokenize 1 $gettok($bvar(&,1,$calc($v1 -1)).text,1,512)
      bcopy -c & 1 & $calc(1+$v1) -1
    }
    else {
      tokenize 32 $bvar(&,1,512).text
      bunset &
    }
    return $1
  }
}

alias -l mds.gxywh {
  if ($bvar(&,0)) {
    if ($bfind(&,1,44)) {
      tokenize 32 $bvar(&,1,$calc($v1 -1)).text
      bcopy -c & 1 & $calc(1+$v1) -1
    }
    else {
      tokenize 32 $bvar(&,1,512).text
      bunset &
    }
    if ($4 != $null) {
      tokenize 32 $iif($1 isnum,$1,0) $iif($2 isnum,$2,0) $iif($3 isnum,$3,0) $iif($4 isnum,$4,0)
      if (%mds.dtype == map) return $1-
      elseif (%mds.dtype == dbu) return $mds.getmap(%mds.dname,$calc($1 *$dbuw) $calc($2 *$dbuh) $calc($3 *$dbuw) $calc($4 *$dbuh))
      else return $mds.getmap(%mds.dname,$1-)
    }
    else bunset &
  }
}

alias -l mds.gid {
  if ($bvar(&,0)) {
    if ($bfind(&,1,44)) {
      tokenize 1 $gettok($bvar(&,1,$calc($v1 -1)).text,1,32)
      bcopy -c & 1 & $calc(1+$v1) -1
    }
    else {
      tokenize 32 $bvar(&,1,64).text
      bunset &
    }
    if ($1 isnum) {
      while ($hget(%mds.dname,$1)) tokenize 1 $calc(1+$1)
      return $1
    }
    else bunset &
  }
}

alias -l mds.gpid {
  if ($bvar(&,0)) {
    if ($bfind(&,1,44)) {
      tokenize 1 $gettok($bvar(&,1,$calc($v1 -1)).text,1,32)
      bcopy -c & 1 & $calc(1+$v1) -1
    }
    else {
      tokenize 32 $bvar(&,1,64).text
      bunset &
    }
    if ($1 isnum) && ($hget(%mds.dname,$1)) return $1
    else bunset &
  }
}

alias -l mds.gcaption {
  unset %mds.noqt
  if ($bvar(&,0)) {
    if ($bvar(&,1) == 34) {
      if ($bfind(&,2,34)) {
        tokenize 1 $bvar(&,2,$calc($v1 -2)).text
        if ($bfind(&,$calc(1+$v1),44)) bcopy -c & 1 & $calc(1+$v1) -1
        else bunset &
        return $1
      }
      else bunset &
    }
    else {
      if ($bfind(&,1,44)) {
        %mds.noqt = 1
        tokenize 1 $bvar(&,1,$calc($v1 -1)).text
        bcopy -c & 1 & $calc(1+$v1) -1
        return $1
      }
      else bunset &
    }
  }
}

alias -l mds.importcontrol {
  set -l %did $1
  set -l %pid $hget(%mds.dname,%did $+ .pid)
  set -l %type $hget(%mds.dname,%did $+ .type)
  set -l %xywh $hget(%mds.dname,%did $+ .xywh)
  set -l %styles $mds.getstyles(%type,$hget(%mds.dname,%did $+ .styles) $hget(%mds.dname,%did $+ .align) $hget(%mds.dname,%did $+ .disable) $hget(%mds.dname,%did $+ .hide)) $wildtok($hget(%mds.dname,styles),notheme,1,32) tabstop
  if (%pid) dll %mds.dcx xdid %mds.dname %pid -c %did $mds.dcxtype(%type) $mds.getpixels(%mds.dname,%xywh) %styles
  else dll %mds.dcx xdialog %mds.dname -c %did $mds.dcxtype(%type) $mds.getpixels(%mds.dname,%xywh) %styles
  did -a mds 4 %type $hget(%mds.dname,%did $+ .id)
  if (%type == edit) {
    dll %mds.dcx xdid %mds.dname %did -r
    if ($hget(%mds.dname,%did $+ .caption)) dll %mds.dcx xdid %mds.dname %did -a $v1
  }
  elseif (%type == icon) dll %mds.dcx xdid %mds.dname %did -C +b $rgb(shadow)
  elseif (!$istok(combo list scroll,%type,32)) dll %mds.dcx xdid %mds.dname %did -t $hget(%mds.dname,%did $+ .caption)
}

on *:dialog:mds:menu:601: {
  clipboard
  %mds.output = clipboard -an
  mds.generate
}

on *:dialog:mds:menu:602: {
  if ($mds.checkdir($nofile($hget(mds.cfg,export)))) set -l %dir $v1
  elseif ($mds.checkdir($nofile($gettok($hget(mds.cfg,recent),1,1)))) set -l %dir $v1
  else set -l %dir $mircdir
  set -l %file $sfile($+(%dir,$iif($hget(%mds.dname,name) != $null,$v1,new_table),.mrc),Export file to...,Export)
  if (!$sfstate) && (%file) {
    hadd mds.cfg export $nofile(%file)
    if ($fopen(mds)) .fclose mds
    .fopen -o mds $qt(%file)
    %mds.output = .fwrite -n mds
    mds.generate
    .fclose mds
  }
}

alias -l mds.checkdir if ($isdir($1)) return $1

on *:dialog:mds:menu:604:mds.generatecode

alias -l mds.generatecode {
  window -zdk0n @MDS -1 -1 800 600
  clear @MDS
  titlebar @MDS code - %mds.dname
  %mds.output = aline @MDS
  mds.generate
  window -a @MDS
}

menu @MDS {
  Copy code: {
    clipboard
    set -l %x 1
    while ($line(@MDS,%x)) {
      clipboard -an $v1
      inc %x
    }
  }
  -
  Regenerate:mds.generatecode
  -
  Close:window -c @MDS
}

alias -l mds.generate {
  window -c @mds.tmp
  window -h @mds.tmp
  unset %mds.tab
  noop $hfind(%mds.dname,*?.id,0,w,mds.addline $hget(%mds.dname,$1) $gettok($1,1,46)))
  %mds.output dialog $iif($istok($hget(%mds.dname,styles),local,32),-l) $iif($hget(%mds.dname,name) != $null,$v1,new_table) $chr(123)
  set -l %caption $hget(%mds.dname,caption)
  if (%caption != $null) %mds.output title $iif($hget(%mds.dname,noqt),%caption,$qt(%caption))
  if ($hget(%mds.dname,iconfile)) %mds.output icon $+($v1,$iif($hget(%mds.dname,iconindex) isnum 1-,$chr(44) $v1))
  tokenize 32 $hget(%mds.dname,xywh)
  %mds.output size $iif($1 isnum,$1,-1) $iif($2 isnum,$2,-1) $3-4
  %mds.output option map $remtok($hget(%mds.dname,styles),local,0,32)
  filter -wktu 1 32 @mds.tmp mds.outputcontrol
  window -c @mds.tmp
  unset %mds.parent
  if ($hget(%mds.dname,tab)) mds.generatetabs
  tokenize 1 $hget(%mds.dname,menu.root)
  mds.outputmenu_1 $*
  if (%mds.output == clipboard -an) clipboard -a $chr(125)
  elseif (%mds.output == .fwrite -n mds) .fwrite mds $chr(125)
  else %mds.output $chr(125)
}

alias -l mds.addline if (!$hget(%mds.dname,$2 $+ .pid)) && ($hget(%mds.dname,$2 $+ .type) != tab) aline @mds.tmp $1-

alias -l mds.generatetabs {
  tokenize 32 $sorttok($hget(%mds.dname,$v1 $+ .children),32,n)
  mds.generatetab $*
}

alias -l mds.generatetab {
  set -l %did $1
  set -l %caption $hget(%mds.dname,%did $+ .caption)
  set -l %id $hget(%mds.dname,%did $+ .id)
  if (%mds.tab) %mds.output tab $+($iif($hget(%mds.dname,%did $+ .noqt),%caption,$qt(%caption)),$chr(44) %id)
  else {
    inc %mds.tab
    tokenize 32 $hget(%mds.dname,$hget(%mds.dname,tab) $+ .xywh)
    %mds.output tab $+($iif($hget(%mds.dname,%did $+ .noqt),%caption,$qt(%caption)),$chr(44) %id,$chr(44) $1-)
    tokenize 32 $dll(%mds.dcx,_xdid,%mds.dname %did dpos)
    %mds.cx = $round($calc($1 /$hget(%mds.dname,mapw)),0)
    %mds.cy = $round($calc($2 /$hget(%mds.dname,maph)),0)
  }
  set -l %children
  tokenize 32 $hget(%mds.dname,%did $+ .children)
  while ($1) {
    %children = $addtok(%children,$hget(%mds.dname,$1 $+ .id) $1,1)
    tokenize 32 $2-
  }
  %children = $sorttok(%children,1,n)
  tokenize 1 %children
  mds.outputcontrol $*
}

alias -l mds.outputcontrol {
  set -l %did $gettok($1-,2,32)
  set -l %type $hget(%mds.dname,%did $+ .type)
  set -l %id $hget(%mds.dname,%did $+ .id)
  set -l %pid $hget(%mds.dname,$hget(%mds.dname,%did $+ .pid) $+ .id)
  set -l %xywh $hget(%mds.dname,%did $+ .xywh)
  if (%pid) {
    tokenize 32 %xywh
    %xywh = $calc($1 +%mds.cx) $calc($2 +%mds.cy) $3-
  }
  set -l %caption $hget(%mds.dname,%did $+ .caption)
  set -l %styles $iif(%pid,tab $v1) $hget(%mds.dname,%did $+ .styles) $hget(%mds.dname,%did $+ .size) $hget(%mds.dname,%did $+ .align) $iif($hget(%mds.dname,%did $+ .limit) isnum 1-,limit $v1) $iif($hget(%mds.dname,%did $+ .range),range $v1) $hget(%mds.dname,%did $+ .disable) $hget(%mds.dname,%did $+ .group) $hget(%mds.dname,%did $+ .hide) $hget(%mds.dname,%did $+ .result)
  if (%type == icon) %mds.output $+(icon %id,$chr(44) %xywh,$iif($hget(%mds.dname,%did $+ .iconfile),$chr(44) $v1 $+ $iif($hget(%mds.dname,%did $+ .iconindex),$chr(44) $v1)),$iif(%styles,$chr(44) $lower(%styles)))
  elseif ($istok(list combo,%type,32)) %mds.output $+(%type %id,$chr(44) %xywh,$iif(%styles,$chr(44) $lower(%styles)))
  else %mds.output $+(%type $iif($hget(%mds.dname,%did $+ .noqt),%caption,$qt(%caption)),$chr(44) %id,$chr(44) %xywh,$iif(%styles,$chr(44) $lower(%styles)))
}

alias -l mds.outputmenu_1 {
  tokenize 32 $1-
  if (q isin $2) set -l %caption $3-
  else set -l %caption $qt($3-)
  set -l %styles $iif(o isin $2,ok) $iif(c isin $2,cancel)
  if ($hget(%mds.dname,menu. $+ $1)) {
    %mds.output menu $+(%caption,$chr(44) $1,$iif(%mds.parent,$chr(44) $gettok($v1,-1,32)))
    %mds.parent = %mds.parent $1
    tokenize 1 $hget(%mds.dname,menu. $+ $1)
    mds.outputmenu_2 $*
    %mds.parent = $gettok(%mds.parent,-2--,32)
  }
  elseif (!%mds.parent) %mds.output menu %caption $+ $chr(44) $1
  elseif (%caption == break) %mds.output item $+(%caption,$chr(44) $1,$iif(%mds.parent,$chr(44) $gettok($v1,-1,32)))
  else %mds.output item $+(%caption,$chr(44) $1,$iif(%mds.parent,$chr(44) $gettok($v1,-1,32)),$iif(%styles,$chr(44) %styles))
}

alias -l mds.outputmenu_2 {
  tokenize 32 $1-
  if (q isin $2) set -l %caption $3-
  else set -l %caption $qt($3-)
  set -l %styles $iif(o isin $2,ok) $iif(c isin $2,cancel)
  if ($hget(%mds.dname,menu. $+ $1)) {
    %mds.output menu $+(%caption,$chr(44) $1,$iif(%mds.parent,$chr(44) $gettok($v1,-1,32)))
    %mds.parent = %mds.parent $1
    tokenize 1 $hget(%mds.dname,menu. $+ $1)
    mds.outputmenu_1 $*
    %mds.parent = $gettok(%mds.parent,-2--,32)
  }
  elseif (!%mds.parent) %mds.output menu %caption $+ $chr(44) $1
  elseif (%caption == break) %mds.output item $+(%caption,$chr(44) $1,$iif(%mds.parent,$chr(44) $gettok($v1,-1,32)))
  else %mds.output item $+(%caption,$chr(44) $1,$iif(%mds.parent,$chr(44) $gettok($v1,-1,32)),$iif(%styles,$chr(44) %styles))
}

on *:dialog:mds:menu:605: {
  set -l %file $qt($scriptdirMDS.tmp)
  if ($fopen(mds)) .fclose mds
  .fopen -o mds %file
  %mds.output = .fwrite -n mds
  mds.generate
  .fwrite -n mds
  set -l %p mds.preview $+ $ticks
  .fwrite -n mds on *:load:dialog -dm %p $hget(%mds.dname,name)
  .fwrite -n mds $+(on *:dialog:,%p,:init:*:.unload -rs %file)
  .fclose mds
  .load -rs %file
  .remove %file
}