Project

General

Profile

Actions

A mini-texte editor

  • Load packages
  • Define and call a function
  • A structure
  • Create and use a (pseudo) class (Oriented-Object Programming)
  • Manage a window, text and button components
  • Callbacks
  • Default dialog boxes

First, Scol is case sensitive, remember this !
MyVariable, myVariable, myvariABle are differents. MyFunction, myFunction, MYFunction are differents too.
fun is a Scol keyword but Fun is not one.

Create a class

The better way is the creation of a new file. It will contains only the class code.
Like other languages, a class is a type. We can create an object : this is an instantiation of the class. Thus, we must be write a constructor. A class has one or more functions (methods or procedures in other languages), privates or publics.

Here is an example. There are several manners to create / use / write a class in Scol, depending on the context. It is our first step in the OOP.

The full commented source code is available in the repository

/* new type */
struct MyClass = [
    // fields / attributs
    ] mkMyClass;;

/* Private functions */
...
/* public functions */
// The constructor of our class
fun MC_Constructor (...)=
    mkMyClass [...];;

fun MC_GetField1 (objMC)=
    objMC.field1;;

fun MC_SetField1 (objMC, value)=
    set objMC.field1 = value;
    objMC;;
...

Create the graphic interface class

In our project, we need a window, a text field (multi lines) and two buttons. Add the width and the height of the window to put our graphics components.

We define a new type : TextEd

struct TextEd = [
    oWin : ObjWin,
    winWidth : I,
    winHeight : I,
    title : S,
    oText : ObjText,
    oLoad : ObjButton,
    oSave : ObjButton,
    cbLoad : fun [ObjButton TextEd] I,
    cbSave : fun [ObjButton TextEd] I,
    cbClose : fun [] I
    ] mkTextEd;;

The constructor is a simple initialization of the TextEd object :

fun TE_new (width, height, title, funLoad, funSave, funClose)=
    mkTextEd [nil width height nil nil nil nil funLoad funSave funClose];;

Of course, it must return the new object.

To build the user interface, we could do something like that :

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;;

ed is a TextEd object.
See source code files (link above) to know all private and public functions of this example.

The main file

We call the constructor and the builder.

fun main()=
    let TE_new 500 400 "Mini text editor" @_cbLoad @_cbSave @close -> ed in
    TE_create ed;
    0;;

_cbLoad and _cbSave are functions called when the load (save) click button event occurs.
close is called after the destruction of the object.

Note : we could write _cbLoad and _cbSave in our class ...

License : GNU FDL v1.3
Tutorial by iri
Updated by /

Updated by iri about 12 years ago · 3 revisions