/* A part of a mini text-editor Author : iri Web siteb : http://www.irizone.net Version : 1.0 Date : october 2012 License : WTF Public License (http://sam.zoy.org/wtfpl/) */ /* This is the "class" part. First, we should define our Text Editor structure : TextEd It contains the 2d objects and the functions callaed when some events occur. We write the private functions (they should not be called outside this "class"). Next, we write the public functions : - the "constructor" (TE_new) - other functions to build the user interface, get an sub-object, set a value or anything else */ /* Structure : definition */ struct TextEd = [ oWin : ObjWin, // the window object winWidth : I, // the current width of the window winHeight : I, // the current height of the window title : S, // the title of the window oText : ObjText, // the text object oLoad : ObjButton, // the 'load' button object oSave : ObjButton, // the 'save' button object cbLoad : fun [ObjButton TextEd] I, // the 'load' click callback cbSave : fun [ObjButton TextEd] I, // the 'save' click callback cbClose : fun [] I // the 'close' window callback ] mkTextEd;; /* Prototyping */ proto TE_getText = fun [TextEd] ObjText;; proto TE_getWindow = fun [TextEd] ObjWin;; /* Private functions */ /* return the 'load' button sub-object */ fun te_getButtonLoad (ed)= ed.oLoad;; /* return the 'save' button sub-object */ fun te_getButtonSave (ed)= ed.oSave;; /* the window is destroyed */ fun te_end (ed)= // we destroy the 2d graphic objects _DStext TE_getText ed; _DSbutton te_getButtonLoad ed; _DSbutton te_getButtonSave ed; _DSwindow TE_getWindow ed; // we call the user function when the destroy event occurs exec ed.cbClose with []; // we set the TextEd object to nil set ed = nil;; fun te_close (window, ed)= te_end ed;; /* the window is resized : text and buttons should be also resized */ fun te_resize (window, ed, width, height)= set ed.winWidth = width; set ed.winHeight = height; _SIZEtext TE_getText ed ed.winWidth-6 ed.winHeight-26 3 3; _SIZEbutton te_getButtonLoad ed (ed.winWidth-6)/2 20 3 ed.winHeight-23; _SIZEbutton te_getButtonSave ed (ed.winWidth-6)/2 20 (ed.winWidth-6)/2+3 ed.winHeight-23;; /* define the callbacks */ fun te_defineCallback (ed)= // private callbacks _CBwinSize TE_getWindow ed @te_resize ed; _CBwinDestroy TE_getWindow ed @te_close ed; // define the callbacks from the user "class" functions _CBbutton te_getButtonLoad ed ed.cbLoad ed; _CBbutton te_getButtonSave ed ed.cbSave ed; ed;; /* Public functions */ /* Initialize our object (Constructor) width : I : the initial width of the GUI height : I : the initial height of the GUI title : I : the initial title of the GUI funLoad : fun [ObjButton I] I : function called when th user clicks on the 'load' button funSave : fun [ObjButton I] I : function called when th user clicks on the 'save' button TextEd : a new initialized object */ fun TE_new (width, height, title, funLoad, funSave, funClose)= mkTextEd [nil width height nil nil nil nil funLoad funSave funClose];; /* Create the GUI. Note : no test is done on the object creations ... ed : TextEd : a TextEd object TextEd : return the same object */ fun TE_create (ed)= if ed != nil then ( set ed.oWin = _CRwindow _channel nil 0 0 ed.winWidth ed.winHeight WN_NORMAL ed.title; set ed.oText = _CReditText _channel ed.oWin 3 3 ed.winWidth-6 ed.winHeight-26 ET_DOWN|ET_ALIGN_LEFT|ET_HSCROLL|ET_VSCROLL nil; set ed.oLoad = _CRbutton _channel ed.oWin 3 ed.winHeight-23 (ed.winWidth-6)/2 20 0 "Load file"; set ed.oSave = _CRbutton _channel ed.oWin (ed.winWidth-6)/2+3 ed.winHeight-23 (ed.winWidth-6)/2 20 0 "Save file"; te_defineCallback ed ) else ed;; /* Destroy the GUI. ed : TextEd : a TextEd object TextEd : return the same object to nil */ fun TE_close (ed)= te_end ed;; /* Return the window object from a TextEd object */ fun TE_getWindow (ed)= ed.oWin;; /* Return the text object from a TextEd object */ fun TE_getText (ed)= ed.oText;; /* Return the content of the text object from a TextEd object */ fun TE_getTextContent (ed)= _GETtext TE_getText ed;; /* Modify the window title ed : TextEd : a TextEd object title : S : the new title TextEd : return the same TextEd object */ fun TE_setWindowTitle (ed, newtitle)= if ed != nil then ( set ed.title = newtitle; _SETwindowName TE_getWindow ed ed.title ) else nil; ed;; /* Modify the text content ed : TextEd : a TextEd object content : S : the new content TextEd : return the same TextEd object */ fun TE_setTextContent (ed, content)= if ed != nil then _SETtext TE_getText ed content else nil; ed;;