123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150(*********************************************************************************)(* OCaml-Stk *)(* *)(* Copyright (C) 2023-2024 INRIA All rights reserved. *)(* Author: Maxence Guesdon, INRIA Saclay *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* 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 General Public License for more details. *)(* *)(* You should have received a copy of the GNU 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 *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** Dialog windows. *)openTsdlclass['a]dialog?classes(on_return:unit->unit)(window:Window.window)(content_area:Bin.bin)(action_area:unitPack.box)=let()=window#add_class"dialog"inobject(self)valmutablewakener=(None:'aoptionLwt.uoption)methodwindow=windowmethodcontent_area=content_areamethodaction_area=action_areamethodreturn?(with_on_return=true)v=(matchwakenerwith|None->()|Somew->Lwt.wakeup_laterwv;wakener<-None);ifwith_on_returnthenon_return()else()methodadd_text_button?classes?return?kstext=let(b,_)=Button.text_button?classes~pack:(action_area#pack~hexpand:0~hfill:false)~text:text()inlet()=matchreturnwith|None->()|Somef->let_=b#connectWidget.Activated(fun()->self#return(f());)in()inlet()=matchkswith|None->()|Someks->Wkey.addwindow#as_widgetks(fun()->let_=b#activatein())inbmethodrun(f:'aoption->unitLwt.t)=let(t,u)=Lwt.wait()inself#return~with_on_return:falseNone;wakener<-Someu;window#show;let_=window#grab_focus()inlet%lwtv=tinfvmethodrun_asyncf=Lwt.async(fun()->self#runf)methoddestroy=window#closeendtypebehaviour=[`Destroy_on_return|`Hide_on_return|`Modal_forofWindow.window]letdialog?classes?(behaviour=`Destroy_on_return)?flags?rflags?resizable?x?y?w?htitle=letmodal_for=matchbehaviourwith|`Modal_forw->Somew|_->Noneinletw=App.create_window?modal_for?flags?rflags?resizable~show:false?x?y?w?htitleinletvbox=Pack.vbox?classes~pack:w#set_child()inletc_area=Bin.bin~classes:["content_area"]~pack:(vbox#pack~hexpand:1~vexpand:1)()inleta_area=Pack.hbox~classes:["action_area"]~pack:(vbox#pack~hexpand:1~vexpand:0)()inleton_return()=matchbehaviourwith|`Hide_on_return->w#hide|`Destroy_on_return|`Modal_for_->w#closeinletd=newdialog?classeson_returnwc_areaa_areainlet_=w#connectWindow.Close(fun()->letkeep=matchbehaviourwith|`Hide_on_return->true|_->falseind#returnNone;keep)indletsimple_input?classes?behaviour?flags?rflags?x?y?w?h?(ok="Ok")?(cancel="Cancel")?(orientation=Props.Horizontal)?(msg="")?(input=`Line)?(text="")title=letd=dialog?classes?behaviour?flags?rflags?x?y?w?htitleinletc_box=Pack.box~orientation~pack:d#content_area#set_child()inlet_msg=Text.label~pack:(c_box#pack~hexpand:0~vexpand:0)~text:msg()inletget_text=matchinputwith|`Line->lete=Edit.entry~pack:c_box#pack~text()in(fun()->e#text())|`Text->letscr=Bin.scrollbox~pack:c_box#pack()inlettv=Textview.textview~pack:scr#set_child()inlet()=tv#inserttextin(fun()->tv#text())inlet_bok=d#add_text_button~return:(fun()->lets=get_text()inSomes)~ks:(Key.keystateSdl.K.return)okinlet_bcancel=d#add_text_button~return:(fun()->None)~ks:(Key.keystateSdl.K.escape)cancelin(d,get_text)