  rem VDS To C++ Converter v1.0
  rem written by Mike Cherry <mike@sterlingpolice.net>
  rem 
  rem converts dialog code into c++ code. NOTE: converts only
  rem dialog code, no functions, callbacks, or anything. also
  rem its important that you specify the window class. the
  rem c++ code generated was tested under borland c++ 5
  rem 
  rem to use, just copy the dialog code from DIALOG CREATE
  rem to DIALOG SHOW, and paste it in the VDS Code text box
  rem then click convert.
  rem 
  rem only works with the following controls:
  rem 
  rem bitmap
  rem text
  rem single line edit
  rem multi line edit
  rem frame
  rem button
  rem checkbox
  rem radio
  rem combo
  rem list
  rem 
  rem have fun with the code, modify if you want just send me a copy.
  rem i know this documentation and probably the code is pretty bad
  rem but ive been coding for the last 4 hours and im just about
  rem fried out :)

  title VDS To C++ Converter

  option fieldsep,@chr(44)

  dialog CREATE,VDS To C++ Converter,-1,0,500,368,class VDS2CPPMain,resizable
  dialog ADD,STYLE,edtstyle,Courier New,10,B,WHITE,BLACK
  dialog ADD,GROUP,frame1,2,2,496,156,VDS Code
  dialog ADD,EDIT,vdscode,16,8,484,136,,multi,scroll,edtstyle
  dialog ADD,GROUP,frame2,162,2,496,156,C++ Code
  dialog ADD,EDIT,ccode,176,8,484,136,,multi,scroll,edtstyle
  dialog ADD,STATUS,STATUS
  dialog ADD,BUTTON,btnconvert,320,2,64,24,&Convert
  dialog ADD,BUTTON,btnexit,320,434,64,24,E&xit
  dialog SHOW

:Resize
  dialog setpos,frame1,2,2,@fsub(@winpos(#VDS2CPPMain,W),12),156
  dialog setpos,frame2,162,2,@fsub(@winpos(#VDS2CPPMain,W),12),@fsub(@winpos(#VDS2CPPMain,H),240)
  dialog setpos,vdscode,16,8,@fsub(@winpos(#VDS2CPPMain,W),25),136
  dialog setpos,ccode,176,8,@fsub(@winpos(#VDS2CPPMain,W),25),@fsub(@winpos(#VDS2CPPMain,H),260)
  dialog setpos,btnconvert,@fsub(@winpos(#VDS2CPPMain,H),75),2,64,24
  dialog setpos,btnexit,@fsub(@winpos(#VDS2CPPMain,H),75),@fsub(@winpos(#VDS2CPPMain,W),75),64,24

:Evloop
  %e = @event()
  if %e
    goto %e
  end 
  dialog cursor
  goto evloop

:btnconvertBUTTON
  if @null(@dlgtext(vdscode))
    warn "No VDS code to convert."
    goto evloop
  end 
  dialog cursor,wait
  dialog disable,btnconvert
  dialog disable,btnexit
  gosub Sub_ConvertVDSCode
  dialog enable,btnconvert
  dialog enable,btnexit
  goto evloop

:btnexitBUTTON
  goto Close

:Close
  stop 


  rem routines for converting vds code to c++ code
:Sub_ConvertVDSCode

  rem define some actual names for the buffer lists
  rem this makes it easier in case the list numbers
  rem have to change
  %%headers = 1
  %%ctrld = 2
  %%main = 3
  %%controls = 4
  %%end = 5
  %%vds_code = 6
  %%c_code = 7
  %%temp = 8

  list create,%%headers
  list create,%%ctrld
  list create,%%main
  list create,%%controls
  list create,%%end
  list create,%%vds_code
  list create,%%c_code
  list create,%%temp

  rem "backup" the contents of the clipboard
  list paste,%%temp

  clipboard set,@dlgtext(vdscode)
  list paste,%%vds_code

  list seek,%%vds_code,0
  parse "%%junk;%%title;%%top;%%left;%%width;%%height;%%class",@item(%%vds_code)
  %%class = @substr(%%class,7,@len(%%class))

  rem load header info into header list
  dialog set,status,"Generating C++ code..."
  list add,%%headers,"// Window and control code generated by VDS To C++ Converter v1.0"
  list add,%%headers,"// VDS To C++ Converter v1.0 written by Mike Cherry <mike@sterlingpolice.net>"
  list add,%%headers,""
  list add,%%headers,"#include <windows.h>"
  list add,%%headers,"#define NTXFONTHEIGHT(PointSize, hDC) (-MulDiv((PointSize), GetDeviceCaps(hDC, LOGPIXELSY), 72))"
  list add,%%headers,""
  list add,%%headers,"char WinClass[] = "@chr(34)%%class@chr(34)";"
  list add,%%headers,"char WinTitle[] = "@chr(34)%%title@chr(34)";"
  list add,%%headers,"LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);"
  list add,%%headers,""

  rem load main window creation code
  dialog set,status,"Creating main window"
  list add,%%main,"int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)"
  list add,%%main,"{"
  list add,%%main,@tab()"HWND hwnd;"
  list add,%%main,@tab()"MSG msg;"
  list add,%%main,@tab()"WNDCLASS wcl;"
  list add,%%main,""
  list add,%%main,@tab()"// define main window class"
  list add,%%main,@tab()"wcl.hInstance = hInstance;"@tab()@tab()@tab()"//hand to this instance"
  list add,%%main,@tab()"wcl.lpszClassName = WinClass;"@tab()@tab()@tab()"// window class name"
  list add,%%main,@tab()"wcl.lpfnWndProc = WindowFunc;"@tab()@tab()@tab()"//window function"
  list add,%%main,@tab()"wcl.style = 0;"@tab()@tab()@tab()"// default style"
  list add,%%main,""
  list add,%%main,@tab()"wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);"
  list add,%%main,@tab()""
  list add,%%main,@tab()"wcl.hCursor = LoadCursor(NULL, IDC_ARROW);"
  list add,%%main,@tab()"wcl.lpszMenuName = NULL;"@tab()@tab()@tab()"// no menu"
  list add,%%main,""
  list add,%%main,@tab()"wcl.cbClsExtra = 0;"@tab()@tab()@tab()"// no extra"
  list add,%%main,@tab()"wcl.cbWndExtra = 0;"
  list add,%%main,@tab()"//background"
  list add,%%main,@tab()"wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);"
  list add,%%main,""
  list add,%%main,@tab()"// register the window class"
  list add,%%main,@tab()"if(!RegisterClass(&wcl)) return 0;"
  list add,%%main,""
  list add,%%main,@tab()"// create main window"
  list add,%%main,@tab()"hwnd = CreateWindow("
  list add,%%main,@tab()@tab()"WinClass,  //class"
  list add,%%main,@tab()@tab()"WinTitle,  //title"
  list add,%%main,@tab()@tab()"WS_OVERLAPPEDWINDOW,  //window style -normal"
  list add,%%main,@tab()@tab()"CW_USEDEFAULT,  // X Coordiate, default"
  list add,%%main,@tab()@tab()"CW_USEDEFAULT,  // Y Coordiate, default"
  list add,%%main,@tab()@tab()@fadd(%%width,7)",  //width, default"
  list add,%%main,@tab()@tab()@fadd(%%height,4)",  //height, default"
  list add,%%main,@tab()@tab()"NULL,  //no parent window"
  list add,%%main,@tab()@tab()"NULL,  //use menu registered with this class"
  list add,%%main,@tab()@tab()"hInstance,  //handle of this instance of the program"
  list add,%%main,@tab()@tab()"NULL"  //no additional arguments"
  list add,%%main,@tab()");"
  list add,%%main,""
  list add,%%main,@tab()"// font handles"
  list add,%%main,@tab()"HFONT MainFont = CreateFont(NTXFONTHEIGHT(8, GetDC(hwnd)), 0 , 0, 0, 400, 0, 0, 0, ANSI_CHARSET, 0 , 0, DEFAULT_QUALITY, DEFAULT_PITCH , "@chr(34)"MS Sans Serif"@chr(34)");"
  list add,%%main,""
  list add,%%main,@tab()"// set window font"
  list add,%%main,@tab()"SendMessage(hwnd, WM_SETFONT, (WPARAM)MainFont, 0);"
  list add,%%main,""
  list add,%%main,@tab()"// controls"

  rem start converting the rest of the dialog
  dialog set,status,"Converting controls..."
  repeat 
    list seek,%%vds_code,@succ(@index(%%vds_code))
    %%dialog_code = @trim(@item(%%vds_code))
    %%term_string = %%dialog_code
    parse "%%junk;%%type;%%name;%%top;%%left;%%width;%%height;%%caption;%%edtstyle",%%dialog_code

    rem convert button
    if @equal(%%type,"button")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"BUTTON"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert group
    if @equal(%%type,"group")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"BUTTON"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | WS_GROUP | BS_GROUPBOX, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL );"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert lists
    if @equal(%%type,"list")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(WS_EX_CLIENTEDGE, "@chr(34)"LISTBOX"@chr(34)", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert combos
    if @equal(%%type,"combo")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"COMBOBOX"@chr(34)", NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert checks
    if @equal(%%type,"check")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"BUTTON"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert radios
    if @equal(%%type,"radio")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"BUTTON"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert bitmaps
    if @equal(%%type,"bitmap")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"STATIC"@chr(34)", NULL, WS_CHILD | WS_VISIBLE | SS_SIMPLE, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert text controls
    if @equal(%%type,"text")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      list add,%%controls,@tab()%%name" = CreateWindowEx(NULL, "@chr(34)"STATIC"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | SS_LEFT, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 

    rem convert multi-line or single-line text box
    if @equal(%%type,"edit")
      list add,%%ctrld,"HWND "%%name" = NULL;"
      if @equal(%%edtstyle,"multi")
        list add,%%controls,@tab()%%name" = CreateWindowEx(WS_EX_CLIENTEDGE, "@chr(34)"EDIT"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_LEFT, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      else 
        list add,%%controls,@tab()%%name" = CreateWindowEx(WS_EX_CLIENTEDGE, "@chr(34)"EDIT"@chr(34)", "@chr(34)%%caption@chr(34)", WS_CHILD | WS_VISIBLE | ES_LEFT, "%%left", "%%top", "%%width", "%%height", hwnd, 0, hInstance, NULL);"
      end 
      list add,%%controls,@tab()@tab()"if (! "%%name") return FALSE;"
      list add,%%controls,@tab()@tab()"SendMessage("%%name", WM_SETFONT, (WPARAM)MainFont, 0);"
      list add,%%controls,""
    end 
  until @equal(%%term_string,"DIALOG SHOW")

  rem load code that comes after control creation
  dialog set,status,"Generating end code..."
  list add,%%end,@tab()"//Display window"
  list add,%%end,@tab()"ShowWindow(hwnd, nShowCmd);"
  list add,%%end,@tab()"UpdateWindow(hwnd);"
  list add,%%end,""
  list add,%%end,@tab()"//create the message loop"
  list add,%%end,@tab()"while(GetMessage(&msg, NULL, 0, 0))"
  list add,%%end,@tab()"{"
  list add,%%end,@tab()@tab()"TranslateMessage(&msg);"
  list add,%%end,@tab()@tab()"DispatchMessage(&msg);"
  list add,%%end,@tab()"}"
  list add,%%end,@tab()"return msg.wParam;"
  list add,%%end,"}"
  list add,%%end,""
  list add,%%end,"LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)"
  list add,%%end,"{"
  list add,%%end,@tab()"switch(message){"
  list add,%%end,@tab()"case WM_DESTROY: //terminate the program"
  list add,%%end,@tab()@tab()"MessageBox(hwnd, "@chr(34)"Replace this line with code to be executed before the dialog closes"@chr(34)", WinTitle, MB_OK);"
  list add,%%end,@tab()@tab()"PostQuitMessage(0);"
  list add,%%end,@tab()@tab()"break;"
  list add,%%end,@tab()"default:"
  list add,%%end,@tab()@tab()"return DefWindowProc(hwnd, message, wParam, lParam);"
  list add,%%end,@tab()"}"
  list add,%%end,@tab()"return 0;"
  list add,%%end,"}"
  list add,%%end,""

  rem build the final source file
  clipboard set,@text(%%headers)
  clipboard append,@text(%%ctrld)
  clipboard append,@text(%%main)
  clipboard append,@text(%%controls)
  clipboard append,@text(%%end)
  list paste,%%c_code
  dialog set,ccode,@text(%%c_code)

  rem set the clipboard to what it had before runing this program
  clipboard set,@text(%%temp)

  rem cleanup and exit subroutine
  list close,%%headers
  list close,%%ctrld
  list close,%%main
  list close,%%controls
  list close,%%end
  list close,%%vds_code
  list close,%%c_code
  list close,%%temp
  dialog set,status,"Conversion complete."
  exit 
