/* Copyright (C) 2011, Stephane Bisaro, aka iri License : you can do what you want with this source code This code is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */ /* Create a text fields interface with the 2dOS Scol API. These text object are managed by the system (win32 api on MS Windows, GTK+ on GNU/Linux). They are faster than the 2dGraphic api but they are not customizable. Documentation : Text API : http://www.scolring.org/files/doc_html/text_controls.html This GUI has two fields : - an input field (editable line only) - an output field which displays what has been written Few keywords ae predefined to run actions : - load, save the content - reset - change the first line displayed - and more (see below) */ /* Text objects must be inside a window object. All resources are managed (loaded, destroyed, ...) by the system. ObjText gives the simpler text objects. You can not customize them. The resize is not automatic. If the parent window can be resized by the user or the program, you should define a callback (_CBwinSize) and, in the reflex, define the text size behavior. */ typeof window = ObjWin;; // main window typeof cText = ObjText;; // output text field (content) typeof cLine = ObjText;; // input text line var prompt = "$ ";; var fileContent = "tutorials/text/content.txt";; /* Add a string to the output field. _ADDtext just concatenates the previous content with any string. If you want a new line, you must do this explicitely ("\n") Prototype : fun [S] I */ fun addContent (string)= _ADDtext cText prompt; _ADDtext cText string; 0;; /* Set a string to the output field. The older content is deleted automatically. Note : we could set the prompt and the string directly : _SETtext cText strcat prompt string; 0;; Prototype : fun [S] I */ fun setContent (string)= _SETtext cText prompt; _ADDtext cText string; 0;; /* Build a simple help / usage and display it in the output field Prototype : fun [] I */ fun displayHelp ()= let "Welcome in this small example of Scol application\n help : show this help\n save : save the content to a predefined file (old content will be overwritten)\n load : load the old content from a predefined file\n reset : content becomes empty\n top : display the top of the content, if any\n bottom : display the bottom of the content, if any\n up : up to 10 lines\n down : down to 10 lines\n" -> help in addContent help; 0;; /* Save the current content of the output field to a predefined file. _GETtext returns this content. _createpack saves any content in any file, the file must be in a write-reference file only (_getmodifypack) See : http://www.scolring.org/files/doc_html/file_system.html Prototype : fun [] I */ fun saveContent ()= let _GETtext cText -> content in _createpack content _getmodifypack fileContent; 0;; /* Load a saved content and display it in the output field. If any, the old content will be deleted _getpack reads the full content of any file, file must be in a read-reference file (_checkpack) See : http://www.scolring.org/files/doc_html/file_system.html Prototype : fun [] I */ fun loadContent ()= let _getpack _checkpack fileContent -> content in setContent content; 0;; /* The output field becomes empty Prototype : fun [] I */ fun reset ()= setContent ""; 0;; /* Display the top of the content _SETfirstLine sets the first visible line if the text is scrolled If the line is 0 then the top of the text will be visible Prototype : fun [] I */ fun displayTop ()= _SETfirstLine cText 0; 0;; /* Display the bottom of the content _GETlineCount returns the total number of lines The bottom of text will be visible Prototype : fun [] I */ fun displayBottom ()= _SETfirstLine cText (_GETlineCount cText)-1; 0;; /* Up to ten lines _GETfirstLine returns the current first visible line Prototype : fun [] I */ fun displayUp ()= let (_GETfirstLine cText) - 10 -> n in ( set n = if n < 0 then 0 else n; _SETfirstLine cText n; 0 );; /* Down to ten lines See comments above Prototype : fun [] I */ fun displayDown ()= let (_GETfirstLine cText) + 10 -> n in ( set n = if n > _GETlineCount cText then (_GETlineCount cText)-1 else n; _SETfirstLine cText n; 0 );; /* Reflex called when the user hits the return key Some keywords are defined. Il the user enters one of them, a special function is called and ran. Otherwise, the content of the input field is added to the content of output field. In all cases, the input field is reset (empty). Prototype : fun [ObjText I S] I */ fun CBlineOK (obj, user_parameter, content)= if (!strcmp content "help") then displayHelp else if (!strcmp content "save") then saveContent else if (!strcmp content "load") then loadContent else if (!strcmp content "reset") then reset else if (!strcmp content "top") then displayTop else if (!strcmp content "bottom") then displayBottom else if (!strcmp content "up") then displayUp else if (!strcmp content "down") then displayDown else let strcat content "\n" -> string in addContent string; _SETtext cLine ""; 0;; /* Add all graphics components to the main window _CRtext create a no-editable text field : it will be the output field (cText) _CReditLine create an editable text line field : it will be the input field (cLine) You must define the channel where they'll be created, typically, this is the current channel, returned by the Scol function "_channel". After, the mother window where they'll be inside. Next, you give its position and size (position x, position y, width and height). Any flags : here, we add horizontal and vertical scrollbar and a border around the text field. To finish, the initial content is defined (may be nil). Here, _SETfirstLine allows to display the bottom of ouput field and _SETtextFocus gives the focus at this text object. Finaly, a callback is defined when the users validates his input message. Prototype : fun [] I */ fun create2dOs ()= let _GETwindowSizePosition window -> [wWin hWin _ _] in ( set cText = _CRtext _channel window 5 5 wWin-10 hWin-35 ET_HSCROLL|ET_VSCROLL|ET_ALIGN_LEFT|ET_BORDER ">>> Hello\n"; set cLine = _CReditLine _channel window 5 hWin-25 wWin-10 20 ET_DOWN|ET_AHSCROLL|ET_ALIGN_LEFT "Enter your message here"; _SETfirstLine cText _GETlineCount cText; _SETtextFocus cLine; _CBlineOk cLine @CBlineOK 0; 0 );; /* Function called when the main windoww is resized _SIZEtext allows to ... resize a text object because the resize is NOT automatic when the mother window is resized. Prototype : fun [ObjWin I I I] I */ fun CBwinResize (win, user_parameter, newWidth, newHeight)= _SIZEtext cText newWidth-10 newHeight-35 5 5; _SIZEtext cLine newWidth-10 20 5 newHeight-25; 0;; // Destroy the virtual Scol machine - fun [] I fun end ()= _closemachine;; // Function called when the main window is destroyed - fun [ObjWin I] I fun CBwinEnd (win, user_parameter)= // we destroy all graphics components _DStext cText; _DStext cLine; end;; /* Main function. The main window is created. _SETwindowMinSize and _SETwindowMaxSize define the minimal (respectively maximale) size of the window Prototype : fun [] I */ fun main ()= _showconsole; // we create a window in the center of the screen let _GETscreenSize -> [wScreen hScreen] in let [500 437] -> [width height] in let [(wScreen/2)-(width/2) (hScreen/2)-(height/2)] -> [x y] in ( set window = _CRwindow _channel nil x y width height WN_MENU|WN_SIZEBOX " Example : 2D Graphic > Text"; let _GETwindowExSizePosition window -> [wall hall _ _] in ( _SETwindowMinSize window wall hall; _SETwindowMaxSize window wScreen hall; ) ); // we define the window callbacks _CBwinDestroy window @CBwinEnd 0; // when window is destroyed _CBwinSize window @CBwinResize 0; // when window is resized create2dOs; 0;;