/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2012 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/

fun cbCloseEdit(p)=
  let p -> [ctrltitle ctrlbutton1 ctrlbutton2 ctrltext ctrlalign theltselector ctrlvalign ctrlhalign ctrlopacity ctrlfadespeed ctrlshow] in
  let getEdCtrlTextLineValue ctrltitle -> title in
  let getEdCtrlTextLineValue ctrlbutton1 -> button1 in
  let getEdCtrlTextLineValue ctrlbutton2 -> button2 in
  let getEdCtrlEditTextValue ctrltext -> text in
  let getSelectedEdCtrlSelectPos ctrlalign -> textalign in
  let getThemeEltSelectorSave theltselector -> thdefsave in
  let getSelectedEdCtrlSelectPos ctrlvalign -> valign in
  let getSelectedEdCtrlSelectPos ctrlhalign -> halign in
  let ftoi getEdCtrlFloatValue ctrlopacity -> opacity in
  let ftoi getEdCtrlFloatValue ctrlfadespeed -> fadespeed in
  let getEdCtrlCheckState ctrlshow -> show in
    ["title" title]::
    ["button1" button1]::
    ["button2" button2]::
    ["text" text]::
    ["textalign" itoa textalign]::
    ["valign" itoa valign]::
    ["halign" itoa halign]::
    ["opacity" itoa opacity]::
    ["fadespeed" itoa fadespeed]::
    ["show" itoa show]::
    thdefsave;;

fun dynamicedit(ewinstr, inst, viewstr, applybtn)=
  let [455 420] -> [iw ih] in
  let 10 -> ypos in
  (
    setEdWindowSize ewinstr iw ih;
    
    let getPluginInstanceParam inst "title" -> title in
    let if title == nil then "" else title -> title in
    let getPluginInstanceParam inst "button1" -> button1 in
    let if button1 == nil then "" else button1 -> button1 in
    let getPluginInstanceParam inst "button2" -> button2 in
    let if button2 == nil then "" else button2 -> button2 in
    let getPluginInstanceParam inst "text" -> text in
    let if text == nil then "" else text -> text in
    let atoi (getPluginInstanceParam inst "textalign") -> textalign in
    let if (textalign == nil) then 1 else textalign -> textalign in
  
    let atoi (getPluginInstanceParam inst "valign") -> valign in
    let if valign == nil then 1 else valign -> valign in
    let atoi (getPluginInstanceParam inst "halign") -> halign in
    let if halign == nil then 1 else halign -> halign in
    let atoi (getPluginInstanceParam inst "opacity") -> opacity in
    let if (opacity == nil) then 100 else opacity -> opacity in
    let atoi (getPluginInstanceParam inst "fadespeed") -> fadespeed in
    let if (fadespeed == nil) then 0 else fadespeed -> fadespeed in
    let atoi (getPluginInstanceParam inst "show") -> show in
    let if (show == nil) then 1 else show -> show in
     
    let crEdFrameWindow _channel ewinstr 0 0 iw 265 EDWIN_RESIZE_MW nil (loc "OS3DDIALOG_0002") (loc "OS3DDIALOG_0001") -> winfrm in
    let crEdWindow _channel winfrm 0 18 iw 245 WN_CHILDINSIDE|WN_NOBORDER EDWIN_RESIZE_MW nil nil -> winstr in
    let crEdCtrlLabel winstr 10 ((set ypos = 10) + 2) 160 20 (loc "OS3DDIALOG_0003") nil -> labeltitle in
    let crEdCtrlTextLine winstr 180 ypos iw-200 20 title nil EDWIN_RESIZE_MW -> ctrltitle in
    let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DDIALOG_0004") nil -> labelok in
    let crEdCtrlTextLine winstr 180 ypos iw-200 20 button1 nil EDWIN_RESIZE_MW -> ctrlbutton1 in
    let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DDIALOG_0005") nil -> labelcancel in
    let crEdCtrlTextLine winstr 180 ypos iw-200 20 button2 nil EDWIN_RESIZE_MW -> ctrlbutton2 in
    let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 170 20 (loc "OS3DDIALOG_0006") nil -> labeltext in
    let crEdCtrlEditText winstr 10 (set ypos = ypos + 25) iw-20 100 text nil EDWIN_RESIZE_MW -> ctrltext in
    let crEdCtrlLabel winstr 10 (set ypos = ypos + 105) + 2 170 20 (loc "OS3DDIALOG_0007") nil -> labelalign in
    let crEdCtrlSelect winstr 180 ypos iw-200 120 EDWIN_RESIZE_MW -> ctrlalign in
    
    let crThemeEltSelector inst ewinstr nil "dialogBox"::nil iw ih -> theltselector in
    
    let crEdFrameWindow _channel ewinstr 0 0 iw 155 EDWIN_RESIZE_MW nil (loc "OS3DDIALOG_0008") (loc "OS3DDIALOG_0001") -> winfrm in
    let crEdWindow _channel winfrm 0 18 iw 135 WN_CHILDINSIDE|WN_NOBORDER EDWIN_RESIZE_MW nil nil -> winstr in
    let crEdCtrlLabel winstr 10 ((set ypos = 10) + 2) 170 20 (loc "OS3DDIALOG_0016") nil -> labelvalign in
    let crEdCtrlSelect winstr 180 ypos 100 120 EDWIN_RESIZE_MW -> ctrlvalign in
    let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 170 20 (loc "OS3DDIALOG_0017") nil -> labelhalign in
    let crEdCtrlSelect winstr 180 ypos 100 120 EDWIN_RESIZE_MW -> ctrlhalign in
    let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DDIALOG_0009") nil -> labelo in
    let crEdCtrlFloat winstr 180 ypos 100 20 itof opacity 0.0 100.0 1.0 0 nil EDWIN_RESIZE_MW -> ctrlopacity in
    let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DDIALOG_0018") nil -> labelf in
    let crEdCtrlFloat winstr 180 ypos 100 20 itof fadespeed 0.0 1000.0 10.0 0 nil EDWIN_RESIZE_MW -> ctrlfadespeed in
    let crEdCtrlCheck winstr 10 (set ypos = ypos + 25) 280 20 (loc "OS3DDIALOG_0010") EDWIN_RESIZE_MW -> ctrlshow in
    
    let V3DgetDefaultSession viewstr -> sessionstr in
    (
      setEdCtrlCheckState ctrlshow show;
      fillEdCtrlSelect ctrlalign (loc "OS3DDIALOG_0011")::(loc "OS3DDIALOG_0012")::(loc "OS3DDIALOG_0013")::nil;
      selectEdCtrlSelectByPos ctrlalign textalign;
      
      fillEdCtrlSelect ctrlvalign (loc "OS3DDIALOG_0014")::(loc "OS3DDIALOG_0012")::(loc "OS3DDIALOG_0015")::nil;
      selectEdCtrlSelectByPos ctrlvalign valign;
      fillEdCtrlSelect ctrlhalign (loc "OS3DDIALOG_0011")::(loc "OS3DDIALOG_0012")::(loc "OS3DDIALOG_0013")::nil;
      selectEdCtrlSelectByPos ctrlhalign halign;
      
      [mkfun1 @cbCloseEdit [ctrltitle ctrlbutton1 ctrlbutton2 ctrltext ctrlalign theltselector ctrlvalign ctrlhalign ctrlopacity ctrlfadespeed ctrlshow] nil];
    );
  );;