Project

General

Profile

A mini-texte editor » History » Version 3

iri, 10/03/2012 11:11 PM

1 1 iri
h1. A mini-texte editor
2
3 2 iri
* Load packages
4 1 iri
* Define and call a function
5 2 iri
* A structure
6
* Create and use a (pseudo) class (Oriented-Object Programming)
7 1 iri
* Manage a window, text and button components
8
* Callbacks
9
* Default dialog boxes
10 2 iri
11
First, Scol is case sensitive, remember this !
12
MyVariable, myVariable, myvariABle are differents. MyFunction, myFunction, MYFunction are differents too.
13
fun is a Scol keyword but Fun is not one.
14
15 3 iri
h2. Create a class
16 1 iri
17 3 iri
The better way is the creation of a new file. It will contains only the class code.
18
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.
19
20
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.
21
22
The full commented source code is available in the "repository":http://redmine.scolring.org/projects/tutorials/repository/show/mini_editors
23
24
<pre>
25
/* new type */
26
struct MyClass = [
27
    // fields / attributs
28
    ] mkMyClass;;
29
30
/* Private functions */
31
...
32
/* public functions */
33
// The constructor of our class
34
fun MC_Constructor (...)=
35
    mkMyClass [...];;
36
37
fun MC_GetField1 (objMC)=
38
    objMC.field1;;
39
40
fun MC_SetField1 (objMC, value)=
41
    set objMC.field1 = value;
42
    objMC;;
43
...
44
</pre>
45
46
h2. Create the graphic interface class
47
48 2 iri
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.
49 1 iri
50 3 iri
We define a new type : TextEd
51 1 iri
52 3 iri
<pre>
53
struct TextEd = [
54
	oWin : ObjWin,
55
	winWidth : I,
56
	winHeight : I,
57
	title : S,
58
	oText : ObjText,
59
	oLoad : ObjButton,
60
	oSave : ObjButton,
61
	cbLoad : fun [ObjButton TextEd] I,
62
	cbSave : fun [ObjButton TextEd] I,
63
	cbClose : fun [] I
64
	] mkTextEd;;
65
</pre>
66 1 iri
67 3 iri
The constructor is a simple initialization of the TextEd object :
68
69
<pre>
70
fun TE_new (width, height, title, funLoad, funSave, funClose)=
71
	mkTextEd [nil width height nil nil nil nil funLoad funSave funClose];;
72
</pre>
73
74
Of course, it must return the new object.
75
76
To build the user interface, we could do something like that :
77
78
<pre>
79
fun TE_create (ed)=
80
	if ed != nil then
81
	(
82
		set ed.oWin = _CRwindow _channel nil 0 0 ed.winWidth ed.winHeight WN_NORMAL ed.title;
83
		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;
84
		set ed.oLoad = _CRbutton _channel ed.oWin 3 ed.winHeight-23 (ed.winWidth-6)/2 20 0 "Load file";
85
		set ed.oSave = _CRbutton _channel ed.oWin (ed.winWidth-6)/2+3 ed.winHeight-23 (ed.winWidth-6)/2 20 0 "Save file";
86
		te_defineCallback ed
87
	)
88
	else
89
		ed;;
90
</pre>
91
92
_ed_ is a TextEd object.
93
See source code files (link above) to know all private and public functions of this example.
94
95
h2. The main file
96
97
We call the constructor and the builder.
98
99
<pre>
100
fun main()=
101
	let TE_new 500 400 "Mini text editor" @_cbLoad @_cbSave @close -> ed in
102
	TE_create ed;
103
	0;;
104
</pre>
105
106
__cbLoad_ and __cbSave_ are functions called when the load (save) click button event occurs.
107
_close_ is called after the destruction of the object.
108
109
Note : we could write __cbLoad_ and __cbSave_ in our class ...
110 1 iri
111
License : "GNU FDL v1.3":https://www.gnu.org/licenses/fdl-1.3-standalone.html
112
Tutorial by iri
113
Updated by /