by Alan Brack This page was last updated: 16/07/2019 The idea of this guide is to give a quick reference to gui level programmers. It is not extensive and more will be added as and when I get chance. My aim is to keep it simple. Remember it's not just about seasoned programmers. An alphabetical list is included below for easy navigation, there's also a table of contents. All examples should 'cut and paste' to the Red console, If you use an editor be sure to add the Red header: Red [needs: 'view] Red in Alphabetical order - Click on a <<back to top>> link to return here: about, across, add, all, alter, any, append, as-pair, ask, area, at, attempt, backdrop, base, below, change-dir, checksum, circle, clear, collect, compose, complement, continue, copy, data, date, debase, delete, do, does, difference, dir, disabled, divide, draw, drop-down, droplist, either, empty? enbase, exists?, exit, extract, extract/index, extend, false, field, fill-pen, find, flags, focus, font-size, hidden, hint, if, if, if error?, image, insert, integer?, italic, keep, last, length?, line-width, loop, load, pad, panel, parse, pen, pick, poke, polygon, 'popup, positive?, progress, prin, print, probe, put, ??, rich-text, save, same, select, set-focus, size, size? size-text, skip, sort, string?, system/view/screens, slider, sort, sort/reverse, split, split-path, space, style, subtract, suffix?, square-root, sqrt, switch, switch/default, tab-panel, take, take/last, take/part, text, text-list, title, to-binary, to-file, to-hex, to-integer, to-string, to-local-file, to-local-file/full, to-red-file, to-time, to-percent, try, triangle, trim, trim/head, trim/tail, trim/all, trim/with, true, type?, underline, union, unless, unview, unique, other stuff: Just see the contents list below and click on what you need to know - click on a <<back to top>> link to return here: more will be added as and when I get time. Let's StartRed [ ] - This is the first line of a Red script and must exist for your script to work. Here it is with other stuff added. Notice that you can change the default app icon by declaring it here (ensure you have your new .ico file in the folder). The 'needs: 'view' is also important if you want to compile your script. Red [ title: "My Script" author: "Your Name" icon: %myicon.ico needs: 'View To create a Window or Form or whatever you want to call it, you use 'view [ ....blah blah blah.... ]' The script here will create a window with a button on it, click the button to quit. Also note: The semi-colon ; is used to add comments to your scripts. ;;Here we use: 'view' 'size' and 'button' Red [needs: 'view] ;;<-- this block is needed for a GUI script. View [ size 300x100 ;;<-- size your window ;;;; this is a comment button "Hello World" [quit] ;;<-- display a button and do something when its clicked ] ;;Here we use: 'below' 'text' 'font-size' 'font-color' 'at' and 'bold'Red [needs: 'view] View [ below ;;<--This will make items appear below the preceding items, see also 'across' text "Hello World" ;;<-- display the message on your form. text bold font-size 14 "Hello World" ;;<-- change font size and display message. text font-size 16 font-color red "hello world" ;;<-- change font size, declare a colour and display message. at 30x20 text "This text is positioned" ;;<-- place text where you want it. at 100x20 button "Quit" [quit] ;;<-- place button where you want it. ] Red [needs: 'view] View [ t: text "Hello World !" b: button "Size Text" [ print size-text t print size-text b unview ] ] ;; gives the area size of t and b type? 21 ;; integer! type? "Alan" ;; string! type? 27/02/2019 ;; date! repeat n 4 [print n] Red [needs:'view author: Alan Brack date: 02/05/2019]
view [
t: area 100x100 red
on-down [print "Ok you clicked it !" t/text: "OK !"] on-over [print "It's Over !" t/text: "Over !"] ] to-time "010:35" ;; Converts to 10:35:00 to-time [21 42 50] ;; Converts to 21:42:50 num: 256 ?? num ;; will print num: 256 num: 123 get 'num ;; notice the preceding quote ( ' ) it's required here. -------------------------------------------------------------- strg: "Hello" get 'strg ;; again notice the preceding quote ( ' ) s: "Hello" pad s 10 st: "You" pad/left st 10 Red [needs: 'view] view compose [(collect [loop 4 [keep [h5 "Hello"]]])] Red[needs: 'view] view collect [keep 'below loop 4 [keep [h5 "Hello"]]] ;;Here we use: 'flags' 'modal' 'resize' 'no-title' 'no-border' 'no-min' 'no-max' 'no-buttons' 'popup'Red [needs: 'view] View/flags [ h5 "Makes window modal" ] 'modal View/flags [ h5 "Enable window resizing" ] 'resize View/flags [ h5 "No window title bar text" button "Ok" [unview] ] 'no-title View/flags [ h5 "Remove a windows border" button "Ok" [unview] ] 'no-border View/flags [ h5 "Remove minimise button from title bar" ] 'no-min View/flags [ h5 "Remove maximise button from title bar" ] 'no-max View/flags [ h5 "Remove all buttons from title bar" button "Ok" [unview] ] 'no-buttons View/flags [ h5 "Makes window a popup (Windows only)" ] 'popup -------------------------------------- ;; this will make window modal and remove min button and allow resize. Red [ needs: 'view] flags: clear [] append flags 'modal append flags 'no-min append flags 'resize view/flags [text "Hello World" ] flags ;; or this will do the same thing view/flags [text "Hello World" ] ['modal 'no-min 'resize] ;;Here we use: 'focus' and 'set-focus' ;; will set focus of face.;; in this example 'area' has focus. Red [needs: 'view] view [ area focus 200x200 f: field 80x25 ] ;;---------------------------------------- ;;change focus with 'set-focus'. Red [needs: 'view] view [below a: area focus 100x100 f: field 80x25 button "field" [set-focus f f/color: orange a/color: white] button "area" [set-focus a a/color: orange f/color: white] ] ;;font sizes 32 26 22 17 13 Red [needs: 'view] view [below h1 "Hello" h2 "Hello" h3 "Hello" h4 "Hello" h5 "Hello"] ;; see also 'text' Red [needs: 'view] m: make map! ["Jenny" 36 "Sandra" 40 "Summer" 22 "Maddy" 25] print m view/flags [ title "My Map" backdrop brown return button "Extend (add Neil and Louise to map)" [ extend m ["Neil" 45 "Louise" 38 ] print "-----------" print m ] return button "Put (add Alan to the map)" [ put m "Alan" 50 print "-----------" print m ] return button "Select (print Sandras age)" [ print select m "Sandra" ] return button "Put (change Sandras age)" [ put m "Sandra" 35 print "------------" print m ] ] ['modal 'no-min] write-clipboard "hello World !" name: ["Steve" "Susan" "Clara" "Alan"] sort name print name age: [60 33 22 19] sort age print age sort/reverse age print age ;;Type '? sort' at the Red console prompt for full list of refinements. str: "Red is my favorite programming language" parse str [to "my" change "my" "OUR"] print str ;; now 'is' has been changed to 'OUR' parse str [thru "programm" change "ing" "ers" ] print str ;; now 'programming' has been changed to "programmers' Learn about it here: ;;using 'loose', 'on-up', 'offset' and 'mold' ;;boundaries, there's always boundaries. Red [needs: 'view] view [title "Drag/Drop" size 300x200 b: box red "Drag/Drop" loose on-up [ if b/offset/x > 220 [print "X right side boundary" b/offset/x: 220] if b/offset/x < 0 [print "X left boundary" b/offset/x: 1] if b/offset/y > 120 [print "Y bottom boundary" b/offset/y: 120] if b/offset/y < 0 [print "Y top boundary" b/offset/y: 1] b/text: mold b/offset print b/offset ] ] view/flags [ on-close [quit] on-enter [either pwrd/text = "alan51" [unview] [pt/text: "Enter valid password !" pwrd/text: copy "" set-focus pwrd] ] title "Password " size 180x120 pt: text font-size 10 font-color red italic "Enter Password" 180 return pwrd: field font-color blue linen focus 130x20 return button "Continue ...." [either pwrd/text = "alan51" [unview] [pt/text: "Enter valid password !" pwrd/text: copy "" set-focus pwrd] ] ] ['modal 'popup] Red [ ] print checksum %myfile.exe 'MD5 ;; where myfile.exe is a file in your folder print size? %myfile.exe ;; make sure the file exists in the default folder ;;;; So you get the checksum of your file and it's size. ;;;; You can replace 'MD5 with 'SHA1 'SHA256 'CRT32 or 'TCP if needed. Red [needs: view] view [ size 300x200 title "Close from the title bar" on-close [view [ h5 "You just closed a window" ] ] h5 font-color red "Close, Unview and Quit" return text "Using on-close gives control when using a close from the title bar" 200x30 return text "Using unview will terminate back to the Red console" 200x30 return text "Using quit will terminate program completely" 200x30 return button "Click to unview" [unview] button "Click to quit" [quit] ] names: ["Jenny" "Carol" "Laura" "Louise"] select names "Laura" ;;this will find 'Louise' - the element following 'Laura' ;;Refinements: ;; /part - Limit the length of the search. ;; /only - Treat a series search value as a single value. ;; /case - Perform a case-sensitive search. ;; /same - Use "same?" to compare. ;; /any - Use * and ? wildcards in searches. ;; /with - Use * or ? as custom wildcards. ;; /skip - Treat the series as fixed size records. ;; /last - Find the last occurrence of value, from the tail. ;; /reverse - Find the last occurrence of value, from the current index. s1: "abcde" s2: "12345" move s2 s1 ;;moves the first element of s2 to s1 ;;so s1 will be '1abcde' and s2: will be '2345' move s1 s2 ;;now s1 will be 'abcde' again and s2 will be '12345' again move/part s1 s2 3 ;;move the first 3 elements of s1 to s2 st: "my world" insert st "Hello " print st ;;insert refinements are: /part (limit number of values inserted). /only (Insert bock types as single values). /dup (Duplicate inserted values). ;;note: insert is destructive. st: "world" st2: "Hello you" insert/part st st2 6 ;; st will now hold 'Hello world' Red [needs: 'view] view [ rich-text data [i b "Hello" /b font 24 red " Red " /font blue "World!" /i] ] ;;This is for 22Apr2018 builds or later. r: "abcd" replace r "a" "NEW" ;; positive will return true if number is more than zero. ;; negative will return true if number is less than zero. p: positive? 1 print p ;;<-- true p: positive? 0 print p ;;<-- false n: negative? 0 print n ;;<-- false n: negative? -1 print n ;;<-- true ;; Imortant: Duplicates are only included once. union [1 2 55 4] [1 2 3 4] ;;result is [1 2 55 4 3] ;;no duplicates. union/case "abc" "Da" ;;result is "abcD" ;; case sensitive minus the duplicate 'a' Red [needs: 'view] view [field 100x30 hint "Enter some text"] ;;Here we use: 'false' 'true' ;; boolean values.1 = 2 ;;<-- clearly false. 5 = 5.0 ;;<-- this is true. "hello" = "HELLO" ;;<-- also true. ;;anyway, all is quite straight forward. ;; also 'no' is false and 'yes' is true. These are also logic values. ;;Here we use: 'offset'Red [needs: 'view] View [ t: text "Hello World" button "Move text" [t/offset: 20x30] ;;<-- change offset of text. ] ;--------------------------------------------------------- Red [needs: 'view] v: view/no-wait [ h1 "Ok" ] v/offset: 20x20 ;;<-- change offset of view window. ;;Here we use: 'hidden' 'disabled' and 'complement'Red [needs: 'view] view [a: area disabled "Hello" button "enable" [if not a/enabled? [a/enabled?: complement a/enabled?] ] button "disable" [if a/enabled? [a/enabled?: complement a/enabled?] ] ] ;----------------------------------------------------------- Red [needs: 'view] view [t: text hidden "Hello !" b: button disabled "quit" [quit] button "enab/disab" [b/enabled?: complement b/enabled?] button "vis/hid" [t/visible?: complement t/visible? ] ] ;;A time event is generated at each tick Red [Needs: 'View] view [ f: h1 "Red Programming !!" rate 1 on-time [either f/text = "" [ f/text: "Red Programming !!" ] [f/text: "" ] ] ] Red [Needs: 'View] view [h1 blue font-color red "Red Programming !!" on-down [quit] ;; click on text to quit ] ;;More on this here .... https://doc.red-lang.org/en/view.html#_event_names ;;returns the value at the given index n: [12 23 35 47 59 66] p: pick n 4 print p s: "abcdefg" p: pick s 6 print p ;----------- ;;replaces the value at the given index n: [1 2 3 4] poke n 3 9 print n s: "abcde" poke s 3 #"S" print s change-dir %c/users/myfolder/new ;;<-- for example. f: "myScript.red" either suffix? f [ print "Yes"] [print "No"] Red [needs: 'view] view/no-wait [size 800x600 txt: text font-size 20 "Hello World !"] center-face txt ;;<-- center txt in the window. do-events ;;;;; Works with ver. 'red-062.exe' or later. ;;;; Refinements: /wait /show /console /shell /input in /output out /error err call/shell "explorer.exe" call/shell "notepad.exe" call "start/min cmd" ;;<-- cmd prompt minimised. call "start/max cmd" ;;<-- cmd prompt maximised. call "start mkdir myfolder" ;;<-- create new folder. call "start rename myfolder newfolder" ;;<-- Rename the folder. call "start rd newfolder" ;;<--Remove/delete folder. call "start copy nul > myfile.txt" ;;<-- Create an empty file. call "start rename myfile.txt newfile.txt" ;;<--Rename the file. call "start copy newfile.txt mycopy.txt" ;;<--make a copy of a file. call "start/b del newfile.txt" ;;<-- Delete file. Note 'start/b' to close cmd window after execution. call/shell "del newfile.txt" ;;<--Alternatively, this gives same result - delete and close cmd. call "start color fc" ;;<- cmd colour. call "start color 9 9" ;;<--this should give a list of all the 'cmd' colour posibilities. call "start echo Hello World !" <--This will display the 'Hello World' message in the cmd window. call "start ECHO ^G" ;;<--A sound on most pc's. call "start date" ;;<--The date. call "start time" ;;<-- The time. call "start ver" ;;<-- Gives the windows version. call "start dir" ;;<-- display the folder contents. e: [2] either empty? e ["Yes Empty"] ["No not Empty"] f: "H" either empty? f ["Yes Empty"] ["no not Empty"] g: "" either empty? g ["Yes Empty"] ["No not Empty"] helloo: does [ print "Hello " print "World !"] helloo nmb: has [n] [ n: 20 - 2] print nmb ;; try it and you'll get it. view [ text font-color red "hello" text font-color purple "world" return return ;;<-- return text font-color blue "how are you ?" ] Red [needs: 'view] view [title "MyPanel" size 200x200 below panel red [size 100x150 below text red "Panel" check red button "Quit" [quit]] ] last ["me" "you" "us" "them"] ;;<-- will return 'them' last [10 20 30 40] ;;<-- will return 40. L: length? [10 15 20 25] ;;<-- there are 4. print L s: length? "abcdefg" ;;<-- there are 7. print s ;;Here we use: 'title' 'across' 'check' and 'radio'Red [needs: 'view] View [ title "My Program" ;;<-- show this in the title bar. across ;;<-- this will make items appear across from the preceding one. check "My check" font-color green ;;<--display a check box with green text. radio "My radio" font-size 12 ;;<- display a radio button with text size 12. ] ;;Here we use: 'backdrop' 'field' and 'area'Red [needs: 'view] View [ backdrop cyan ;;<-- background colour. t: text cyan ;;<-- make t hold text. name: field ;;<-- create a field. button "< Type something and click" [t/text: name/text] ;;<-- make text show whats in field. a: area ;;<-- create an area. 'a' will equal area. button "< Type something and click" [t/text: a/text] ;;<-- make text show whats in area. ] ;;see examples on my 'Red Programming' page. ;;Here we use: 'image'Red [needs: 'view] View [ image %myImage.jpg ;;<-- display image (make sure image file is in your folder). image %myImage.jpg 400x400 ;;<-- display image to your chosen size. at 300x200 image %myImage.jpg 50x50 ;;<-- place image where you want it and re-size it. ] View [ image %myImage.jpg [Quit] ] ;;<-- click on image to quit. ;;Here we use: 'loop'Red [needs: 'view] loop 4 [ ;;<-- will loop 4 times View [ text "Hello World !" ] ] Red [ ] n: 0 loop 16 [ n: n + 1 if n > 10 [break] ;;<-- break out of loop. print n ] print "Ended using Break" Red [ ] n: 0 loop 16 [ n: n + 1 if n < 10 [continue] ;;<-- continue from 10. print n ] ;---------------------------- Red [ ] n: 0 loop 16 [ n: n + 1 if n > 10 [continue] ;;<-- continue till 10. print n ] Red [] folder: read %. ;;<-- read the default folder in. count: 0 foreach file folder [ ;;<-- outer foreach loop to evaluate folder. foreach ext [".jpg" ".gif" ".png" ".bmp"] [ ;;<-- inner foreach loop to get image file extentions. if find file ext [ print file ;;<-- and print them to console. count: count + 1 ;;<-- keep count. ] ;;<-- end off 'if' block. ] ;;<-- end off inner 'foreach' loop. ] ;;<--end of outer 'foreach' loop. print rejoin [newline "Total Images: " count] ;;<-- reduce and join the values and print. halt ;;<-- halts script. s: "abcdefg" clear s ;;<-- clear / delete contents of s. print s s: "you me us them" clear find s "us" ;;<-- clear / delete from "us" . print s n: [1 2 3 4 5] clear skip n 3 ;;<-- clear / delete from 3. print n ;; both 'function' and 'func' will define a function. ;; where everything in 'function' body is local - and everything in 'func' body is global. ;; 'exit' will exit a function/func returning no value. Red [ ] sums: function [a b] [n1: a + b print n1 ;;<-- local if a < 1 [exit] if b < 1 [exit] n2: a * b print n2 ;;<-- local ] sums 2 5 ;;<-- will return 7 and 10. sums 0 4 ;;<-- will return only 4 before exiting the function. ;---------------------------------------- ;; below is the same 'func' version (global). Red [ ] sums: func [a b] [n1: a + b print n1 ;;<-- global if a < 1 [exit] if b < 1 [exit] n2: a * b print n2 ;;<-- global ] sums 2 5 ;;<-- will return 7 and 10. sums 0 4 ;;<-- will return only 4 before exiting the function. ;; 'any' will return true if at least one condition is met. n: 2 v: 4 if any [n = 2 v > 5] [print true] ;------------------------------------- ;; 'all' will return true if all conditions are met. n: 2 v: 4 if all [n = 2 v = 4] [print true] ;; 'print' outputs to the console window ending with a new line. ;; 'prin' does the same without ending with a new line. ;; 'space' inserts a space. ;; 'probe' prints in a molded form. ;; 'newline' is a char! with value of: #"^/" print "Hello" print "World !" prin "Hello" prin space prin "World !" probe "Hello World !" s: 5 n: 5 either same? s n [print "They are the same"] [print "Not the same"] ;; now try: s: 8 n: 5 either same? s n [print "They are the same"] [print "Not the same"] ;;Here we use: 'forever' 'unview' 'quit' 'if' and 'print'Red [needs: 'view] a: 0 ;; <-- declare a variable/pointer forever [ ;;<-- start a forever loop. a: a + 1 ;;<-- a = a + 1 View [ text "constant loop" button "Not a quit" [unview]] ;;<-- unview window (not quit). if a > 6 [quit] ;; <-- if 'a' more than 6 then quit. print a ;;<-- this will print 'a' to the console window. ] ;------------------------------------- ;; More unview ;; unview/all <-- Close all views. ;; unview/only face <-- Close a given view. ;; where face is 'view to close'. ;;Here we use: 'foreach' to read a block.Red [needs: 'view] foreach info ["Us" "Them" "Me" "You"] [ View [ text info ] ;;<-- display result in a view ] ;; see more 'foreach' here. ;;move through the series one a time. Red [ ] n: [5 10 15 20 25 30] forall n [print n] ;;<-- try it and you'll get it. ;;Here we use: 'no-wait' a refinement of view. and 'wait'Red [needs: 'view] loop 5 [ view/no-wait [ text "loop"] ;;<-- display your window without waiting. wait 1 ;;<-- pause a sec. unview ;;<-- close last opened view. ] ;;Here we use: 'save' 'load' 'write' 'read' 'delete'Red [needs: 'view] view [ below t: text button "Save" [ save %myfile.txt "Some text" t/text: "Saved"] ;;<--Save to folder when button clicked. button "Load" [t/text: load %myfile.txt] ;;<-- load from folder when button clicked. button "Write" [write %myfile2.txt "More text" t/text: "Written"] ;;<-- Write when clicked. button "Read" [t/text: read %myfile2.txt] ;;<--Read when clicked. ] ;;Check out the two files in your folder .... ;;Now delete them .... Red [needs: 'view] view [ button "Delete" [ delete %myfile.txt delete %myfile2.txt ] ] s: suffix? %myscript.red ;;<-- will return ' .red ' print s ;;Here we use: 'load' to convert a string to integer.mystring: "22" ;; <-- a string. probe mystring ;;<--show in console window. mystring: load mystring ;;<--Now mystring is an integer. probe mystring ;;<--show in console window. ;;Here we use: 'mold' returns string representation of a value.Red [needs: 'view] n: 44 ;;<-- make n equal the number 44. s: mold n ;;<-- use 'mold' to get a string representation of the value. view [ text s ] ;;<-- now we can display it. ;;append: Appends a value to the tail of a series and returns the series head. ;;form: Converts a value to a string. ;;Here we use 'Append' and 'form' to make the string 'Time' and the value 'now/time' live together in a new string variable. tim: append form "Time: " now/time ;;<-- set tim variable with time probe tim ;;<-- show the result in the console window. ;;Here we use: 'now/time' to show the hour, minutes and seconds.;;with the use of 'append and form'. Red [ "Time" needs: view] t: now/time hr: append form "Hours: " t/hour ;;<-- get the hour mn: append form "Minutes: " t/minute ;;<-- get the minutes sc: append form "Seconds: " t/second ;;<-- get the seconds tim: append form "The Time: " now/time ;;-- get the complete time view [ title: "The Time" ;;<--open a view and give it a title below text font-size 16 hr ;; <--display the hour text font-size 16 mn ;;<--display the minute text font-size 16 sc ;;<--display the second text font-size 16 font-color red tim ;;<-- display the time ] ;;<-- end of view d: now/date print d print now/time/day print now/time/month print now/time/year ;; July 07/2017 build or later for this. ;;Here we use: 'request-file' 'request-dir' 'request-font' pop-up system dialog boxes.request-file/file %myfile.txt ;;<-- pop-up 'Open' dialog box asking for myfile.txt. request-file/file/save %myfile.txt ;;<-- pop-up a 'Save' dialog box for myfile.txt request-dir ;;<-- pop-up a 'folder' dialog box. request-font ;;<-- pop-up a 'font' dialog box. ;;Here we use: 'if error?' and 'try' we force an error by dividing by zero.if error? try [0 / 0] [view [text "Divide by zero error !"]] ;;to make things clear: ;;the first set of square brackets hold the error expression, ;;and the second set of square brackets deal with it. i.e. if error? try [ blahh blahh] [do something here if it's an error] ;;Here we use: 'text-list' 'copy' 'data'Red [needs: view] view [ text-list data ["Me" "You" "Us" "Them"] ] ;;Or copy a list Red [needs: view] myData: copy ["Me" "You" "Us" "Them"] view [ text-list data myData ] :: Just a tip.... When you assign a word to another word, or if you prefer, ;; a variable to another variable, make sure you 'copy' it. ;;;; myvar: copy myvar2 instead of myvar: myvar2 ;;Here we use: 'if' 'not' and 'exists?'if not exists? %noFile.dat [print "File not in folder"] ;;this prints a message to console 'if' file is 'not' in folder. ;;Here we use: 'do' 'uppercase' and 'lowercase'Red [needs: 'view] view [ t: text do [t/text: uppercase "hello"] ;;<-- change 'hello' to uppercase. ] view [ t: text "HELLO" button "change" [t/text: lowercase t/text] ;;<-- change 't' to lowercase. ] ;;Here we use: '+' '-' '/' '*' 'add' 'subtract' 'divide' 'multiply' '%'a: 4 + 4 print a ;;<-- add and print to console. a: add 4 4 print a ;;<--gives same result a: 5 - 2 print a ;;<-- subtract and print to console. a: subtract 5 2 print a ;;<-- gives same result a: 10 / 6 print a ;;<-- divide and print to console. a: divide 10 6 print a ;;<-- gives same result a: 4 * 2 print a ;;<-- multiply and print to console. a: multiply 4 2 print a ;;<-- gives same result. a: 13 % 6 ;; goes 2, so 'a' will equal 1. print a s: square-root 100 ;; <-- will return the square root. s: sqrt 100 ;; <-- will do the same. integer? 6 ;; <-- will return true. integer? 4.5 ;; <-- will return false. n: 25 either integer? n [print "Yes, its an Integer"] [print "No, its not an Integer"] ;; <-- will print 'Yes' n: 23.5 either integer? n [print "Yes, its an Integer"] [print "No, its not an Integer"] ;; <-- will print 'No' ;;Here we use: 'string?' Returns true if a string.string? "Hello" ;; <-- will return true. string? 44 ;; <-- will return false. s: "Hello" either string? s [print "Yes, its a string"] [print "No, its not a string"] ;; <-- will print 'Yes' s: 44 either string? s [print "Yes, its a string"] [print "No, its not a string"] ;; <-- will print 'No' ;;Here we use: 'system/view/screens' to find the screen size.Red [needs: 'view] scrn: system/view/screens ;; <-- get the screen size. s: append form "screen size = " scrn/1/size ;;<-- make it view friendly. view [ text s ] ;;<-- display it. ;;Here we use: 'either'a: 10 either a = 9 [print "a equals 9"] [print "a does not equal 9"] ;;So if the condition is true eval first(true) block else eval second(false) block. ;;In this case 'a' does not equal 9 - so the second block is evaluated. ;;Here we use: 'as-pair' Combine X and Y values into a pair.x: 20 y: 10 p: as-pair x y ;;<-- make them a pair print P ;;Here we use: 'until'Red [needs: 'view] n: 0 until [ n1: mold n view/no-wait [text n1 ] wait 1 n: n + 1 n > 5 ] print "Done" ;;Here we use: 'while'c: 1 while [c < 10] [ print c: c + 1] ;;<-- condition block and body block print "done" ;;Remember 'draw' is a massive dialect (triangle, box, circle, arc, curve, etc, etc, etc ....) Red [needs: 'view] ;; circle view [base 200x200 draw [pen yellow circle 100x100 40]] ;; square view [base 200x200 draw [pen red box 60x60 140x140]] ;; triangle view [base 200x200 draw [pen blue triangle 60x110 140x110 100x50]] ;; polygon view [base 200x200 draw [pen cyan polygon 100x60 120x120 60x140]] ;; box with fill-pen view [base 200x200 draw [fill-pen red box 60x60 140x140]] ;; circle with fill-pen view [base 200x200 draw [fill-pen yellow circle 100x100 40]] ;; triangle with fill-pen view [base 200x200 draw [fill-pen blue triangle 60x110 140x110 100x50]] ;; line with line-width set to 10 view [base 200x200 draw [line-width 10 pen blue line 40x100 150x120]] ;;Try this link also, for more on 'draw': ;;Here we use: 'tab-panel' Red [needs: 'view] view [ Title "Tab-Panel Example" tab-panel 200x100 [ "Tab 1 " [at 30x30 text font-size 16 font-color red "Panel 1 ...."] "Tab 2 " [at 30x30 text font-size 16 font-color blue "Panel 2 ...."] "Tab 3 " [at 30x30 text font-size 16 font-color green "Panel 3 ...."] ] ] ;;Here we use: 'overlap?' and 'within?' .... Collision detection.if overlap? a b [print "collided"] ;;;; <- where a and b are the colliding faces. ;----------------------------------------------------------------- if within? point offset size [print "collided"] Note: point is xy position- offset is offset of area - size is size of area ;;Here we use: 'trim' 'trim/head' 'trim/tail' 'trim/all' 'trim/with';;;; Removes spaces and/or chars from a string ;;;; str: " Hello World !. " ;;<-- str has 4 leading and 4 trailing spaces & 1 space in the middle. trim str ;;<-- remove leading and trailing spaces. trim/head str ;;<-- remove leading spaces only. trim/tail str ;;<-- remove trailing spaces only. trim/all str ;;<-- remove all spaces. trim/with str "!." ;;<-- remove specified chars from a string. make-dir %newDir/ ;;<--create a new folder. make-dir/deep %newDir/newSubDir/ ;;<--create a new folder with a sub folder. ;;Here we use: 'dir' ;;<--lists the current dir. Or list the full path.dir ;;<--simple - will list the current dir. dir %/c/folder/ ;;<--list the full path (where '%/c/folder/') is your path. ;;Here we use: 'case' 'case/all' ;;<--evaluates a block.;;;; evaluates the first true condition only ;;;; num: 200 case [ num > 200 [print "num is more than 200"] num < 200 [print "num is less than 200"] num > 100 [print "num is more than 100"] num = 200 [print "num is 200"] true [print "Yes its true"] ] ;--------------------------------------------------- ;;;; evaluates all true conditions ;;;; num: 200 case/all [ num > 200 [print "num is more than 200"] num < 200 [print "num is less than 200"] num > 100 [print "num is more than 100"] num = 200 [print "num is 200"] true [print "Yes its true"] ] num: [5 3 9 7 6 4 8] sort num ;;<--sort ascending. sort/reverse num ;;<--sort descending. strng: ["S" "E" "D" "N" "F" "X" "W"] sort strng ;;<--sort string ascending. sort/reverse strng ;;<--sort string descending. ;;Here we use: 'alter' 'find'a: "Hello" ;;<--make 'a' = 'Hello'. alter a " World !" ;;<-- appends a to 'Hello World !' (World ! is not there, so it is added) alter a "!" ;;<-- deletes '!' from a (when a value exists it is removed) alter a "!" ;;,-- puts '!' back (when a value does not exist it is appended) ;-------------------------------------------- a: ["you" "me" "us"] alter a "them" ;;<--add 'them' alter a "us" ;;<--remove 'us' alter a "us" ;;<--add 'us' back at the end. find a "us" ;;<--returns the series from the position value is found. See other 'find' refinements ! also see more 'find' here. --------- txt: "Some text to look at" if find txt "look" [print "found it"] -------- a: "you me us them" change a "yes" ;;<--replace the first 3 chars with 'yes' print a ;;<--now print result to console. ;---------------------------------------- n: [11 12 13 14] change n 22 ;;<--replace 11 with 22. print n ;;<--print result to console. ;---------------------------------------- n: [12 13 14 15] change n [88 99] print n extract [1 2 3 4 5] 2 ;;<--extracts every 2nd number from the block. extract "Hello World" 3 ;;<--extracts every 3rd char from string. extract/index "abcdefghijklm" 2 4 ;;<--start from offset 4 and extract every 2nd char. g: extract/index "abcdefghijklm" 2 3 print g strng: "you and me" split strng " " d: [2 4 6 8 10] remove d ;;<--remove the first item from series. s: "abcdefgh" remove s ;;<--remove the first item from string. remove/part d 2 ;;<--remove the first two items. remove/part s 3 ;;<--remove the first three items. ;;------------------------- s: "Hello Red world" reverse s remove/part s 5 reverse s print s s: remove split-path %desktop\ufo1.png ;;<--using remove gives us the target. print s ;; see also 'remove' and 'remove/part' to-file "myfile.txt" ;;<-- Convert to file! value. == %myfile.txt to-local-file %/c/users/myfile.dat ;;<-- red file to local file format. == "c:\users\myfile.dat" to-local-file/full %myfile.dat ;;<-- red file to full path. == "c:\users\desktop\redthings\myfile.dat" to-red-file "c:\users\myfile.dat" ;;<-- local file format to Red file. == %/c/users/myfile.dat Red [needs: 'view] view [title "Text-List" t: text "Click on a month ->" text-list 100x100 data[ ;;<--start text-list "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"] ;;<--end text-list [t/text: pick face/data face/selected] ;;<--what did we select ] ;;<--end view ------------------------------------- ;Pick from block p: [a b c d e f g] g: pick p 3 print g ;Pick from string s: "abcdefghijklm" g: pick s 8 print g ;;see also here for more; 'pick' Red [needs: 'view] view [title "drop-down" t: text "-->" drop-down "Choose one" data ["You" "Me" "Us" "Them"] on-change [t/text: pick face/data face/selected] ] Red [needs: 'view] view [title "Drop-List" t: text "Choose and click ->" drop-list data ["You" "Me" "Us" "Them"] select 1 [t/text: pick face/data face/selected] ] ;;<--end view ;;Here we use: 'slider' 'progress' 'on-change';; display a 'slider' and a 'progress' and use 'on-change' to update progress data. Red [ needs: 'view] view [title "slider, progress bar" below slider 100x20 data 10% on-change [p/data: face/data] p: progress 100x20 10% ] ;;Here we use: 'style' 'group-box' 'reduce' 'origin' 'attempt';; We create a 'group-box' with 'field's, 'button's, and 'text'. notice 'reduce' and 'attempt'. ;; 'reduce' evaluates the block to be saved .... 'attempt' kills any error caused by loading a none field. Red [needs: 'view] view [ style txt: text ;; <-- sets a new style. group-box "My Group Box" 1 [origin 40x40 ;; <-- start of group-box and size it. txt "Name" name: field 200 txt "Address1" addrs1: field 200 txt "Address2" addrs2: field 200 button "save" [save %mygroupbox.txt reduce [name/text addrs1/text addrs2/text ] t/text: "*Saved*"] button "load" [attempt [set [nm ad1 ad2] load %mygroupbox.txt] attempt [name/text: nm] attempt [addrs1/text: ad1] attempt [addrs2/text: ad2] t/text: " *Loaded* "] t: text font-size 16 font-color red " " ] ] ;;Here we use: 'what' 'about' 'os-info'Typing 'what' at the Red console prompt will give you .... .... brief information about all Red functions. Typing 'about' at the Red console prompt will give you .... .... the Red version currently in use .... Typing 'os-info' at the Red console prompt will give you .... .... detailed operating system version information. .... unique "abcdabcdefg" ;;<-- result will be >> "abcdefg" unique [1 2 3 4 1 2 3 4 5 6 7 8 ] ;;<-- result will be >> [1 2 3 4 5 6 7 8] b: "Hello" b: to-binary b ;;<-- change to binary. to binary! does the same. print b b: to-string b ;;<-- change back to string. to string! does the same. print b n: "44" n: to-integer n ;;<-- change to integer. to integer! does the same. n: to-string n ;;<-- and back to string. ;;also check out my 'Red Programming' page (example 18) ;;Here we use: 'ask'ask "Enter your name: " ;;<--copy this to the console. n: ask "Enter your name: " ;;<--now n will hold the string. n: 2 either zero? n [print "yes its zero"] [print "no it aint zero"] Red [title: "MyColours" needs: 'view] view [text font-color blue "Hello" ;;<--set text colour. text orange "Hello" ;;<--set text background. ] ;;list of colours: ;;aqua, beige, black, blue, brick, brown, coal, crimson, cyan, forest, gold, gray, green ;;ivory, khaki, leaf, linen, magenta, maroon, mint, navy, olive, orange, papaya, pewter ;;pink, purple, red. ;;Here we use: 'random' 'random/seed' 'random/only'Red [] random/seed now/time ;;<-- seed the random generator. loop 9 [ r: random 10 ;;<-- get a random number. print r ] Random/only ["one" "two" "three" "four"] ;;<--picks a random value from a series. ;;Here we use: 'switch' 'switch/default';; in this example we use 'switch' to evaluate random numbers to find a match. Red [] random/seed now/time loop 8 [ r: random 5 ;; get a random number print r switch r [1 [print "Number One"] 2 [print "Number Two"] 3 [print "Number Three"] 4 [print "Number Four"] 5 [print "Number Five"] ] ] ;------------------------------------------- ;; here 'switch/default' is used. 7 random numbers, with only 5 possible matches. Red [] random/seed now/time loop 8 [ r: random 7 print r switch/default r [1 [print "Number One"] 2 [print "Number Two"] 3 [print "Number Three"] 4 [print "Number Four"] 5 [print "Number Five"] ] [print "No Number matched"] ;; <-- default message. ] ;;Here we use: 'max' 'min'max 420 610 min 420 610 max "abcd" "dcba" min "dcba" "abcd" max [1 2 3] [3 2 1] ;; if any of these are not true they will return none. if 2 < 3 [print "Yes"] ;;<-- will print message as it is true. a: 3 if a <= 3 [print "Yes"] ;;<-- will print message as it is true. a: 3 if a <> 2 [print "yes"] ;;<-- will print message as it is true. a: 3 b: 3 if a = b [print "Ok, Yes"] ;;<-- will print message as it is true. a: 3 if a > 4 [print "ok"] ;;<-- will return none. a: 3 either a <= 2 [print "yes its less than or equal to"] [print "no its not"] s: "yes" either s = "yes" [print "It's true"] [print "It's false"] a: 5 unless a > 5 [print "Not more than 5"] ;;<-- if not more than 5, print message to console. ;; returns a copy of the block after evaluating its expressions. r: reduce [2 + 4] ;;<-- puts 6 into a block. r: reduce [2 + 4 3 + 4] ;;<-- puts 6 and 7 into a block. Red [] t: "Hello" t2: "Red" t3: "World" write/lines %tfile.txt reduce [ t t2 t3 ] ;;<-- this works because of reduce. s: "abcdefg" skip s 2 r: skip s 4 print r p: to-percent 2 ;;<-- does what it says. r: remainder 10 4 ;;<-- 4 into 10 goes 2 and 2 left over. print r browse https://google.co.uk browse http://www.mycode4fun.co.uk/red-apps hx: to-hex 32 ;;<-- does what it says. ;; enbase will create a binary string (base 64) e: enbase "Hello" d: debase e ;;<-- to binary. to string! d ;;<-- and back to a string. ;; just try it and you'll get it. t: [12 13 14 15 16 17] take t take/last t take/part t 3 Red [needs: 'view title: "fonts" ] view [below text italic "Hello" text bold "Hello" text font-color purple underline "Hello" text yellow "Hello" text font-color red "Hello" text font-size 16 "Hello" text font-name "comic sans ms" font-size 14 italic "Hello" text italic bold font-color green font-size 16 "Hello" ] ;;----------------------------------- Red [needs: 'view] view [ h5 underline "Hello World"] ;; detect keypress: 'x' 'q' 'space bar'.... should be easy to adapt for other keys. Red [ needs: 'view] ;;;; get a keypress ;;;; view/options [ t: text "hit 'space', 'x', or 'q' keys !"][ actors: object [on-key: func [key event] [ if event/key = #"x" [t/text: "'x' key pressed."] if event/key = #"q" [t/text: "'q' key pressed."] if event/key = #" " [t/text: "'space bar'pressed."] if event/key = #"^M" [t/text: "Return. key pressed"] ] ] ] ;; see also 'on-enter' for Return key press ;; see also 'function' for a better example of 'func' ;; hit a key and see what it is .... view [field on-key-down [print mold event/key ] ] ;;note: 'red.exe' should be the red version you have in your folder. > red.exe myscript.red ;;<-- will interpret script, where 'myscript.red' is your script. > red.exe -c myscript.red ;;<-- will compile script to Dos. > red.exe -c -t windows myscript.red ;;<-- will compile script to Windows. ;;See my 'AutoRed' app to make this much easier. ;;Or use my 'RedEd' text editor with interpret and compile options. ;;Both can be found on this page: 'Red Apps' Development Mode and Release Mode. With regard to compilation of Red scripts. hope this is helpful. At this point there is 'Development Mode' and 'Release mode' 'Development Mode' is the default. The command line would be: 'red.exe -c yourScript.red' The first time you try it it will build the 'libRedRT.dll' in your folder. After that you'll get lightning fast compiles, but remember the resulting exe will require the 'libRedRT.dll' to work. 'Release Mode' generates a 'stand alone' no need for 'libRedRT.dll' Command line will be: 'red.exe -c -r yourScript.red' The compile time is a lot longer in release mode. Note also that a 'release mode' -r compile will be required in certain circumstances .... i.e. when using #include .... etc .... |