The Ring programming language version 1.8 book - Part 79 of 202
1. Ring Documentation, Release 1.8
63.8 Window Flags
From the Window properties we can open the Window Flags window.
63.8. Window Flags 751
2. Ring Documentation, Release 1.8
63.9 Entering Items
For some controls like the List Widget we can enter items separated by comma ‘,’
63.10 Using Layouts
1. To use layouts, At first add the layout control to the window.
2. Use the window “Set Layout” property to determine the main layout.
3. From the layout properties determine the controls and the layout type.
63.11 More Samples and Tests
Check the folder : ring/applications/formdesigner/tests
Online : https://github.com/ring-lang/ring/tree/master/applications/formdesigner/tests
63.9. Entering Items 752
3. CHAPTER
SIXTYFOUR
MULTI-LANGUAGE APPLICATIONS
There are many ways to create multi-language Ring application!
In this chapter we will learn about using the String2Constant tool
64.1 Using String2Constant
Starting from Ring 1.8 we have the String2Constant application
You will find this tool in the ring/samples/tools/string2constant folder
Using this tool we can convert the source code to be based on constants instead of string literals
Then we can store constants in separate source code files that we can translate to different languages
Where we can have special file for each language, like (English.ring, Arabic.ring and so on)
Using this simple tool, the Form Designer is translated to Arabic language too just as an example.
753
4. Ring Documentation, Release 1.8
64.2 Form Designer Translation
You will find the form designer application in the ring/applications/formdesigner folder
The files used for translation are stored in the ring/applications/formdesinger/translation folder
You will find two files
• Arabic.ring
• English.ring
You can check these files to get an idea about constants definition.
The next section from the English.ring file
64.2. Form Designer Translation 754
5. Ring Documentation, Release 1.8
T_LANGUAGE = "english"
T_LAYOUTDIRECTION = 0 # Left to Right
T_FORMDESIGNER_FORMDESIGNER = "Form Designer"
T_FORMDESIGNER_FORMTITLE = "Form1"
T_FORMDESIGNER_FILE = "File"
T_FORMDESIGNER_NEW = "New"
T_FORMDESIGNER_OPEN = "Open"
T_FORMDESIGNER_SAVE = "Save"
T_FORMDESIGNER_SAVEAS = "Save As"
T_FORMDESIGNER_CLOSE = "Close"
The form designer source code files will use these constants instead of typing the string literals
the next section from the formdesigner/mainwindow/formdesignerview.ring
# Create the Main Window and use the Mdi Area
win = new qMainwindow() {
setWindowTitle(T_FORMDESIGNER_FORMDESIGNER) # "Form Designer"
setcentralWidget(this.oArea)
setLayoutDirection(T_LAYOUTDIRECTION)
}
• Using comments we can write the string literal to get more readable code.
• Using setLayoutDirection() method we can set the window direction to be Right To Left.
• Using the Load command, We can determine which translation file to use.
64.3 Forms Translation
After creating the form using the Form Designer, the View class will be generated.
We don’t modify the view class, We just add the translation through the Controller class.
For example, we have the form file : ring/formdesigner/selobjects/selobjects.rform
64.3. Forms Translation 755
6. Ring Documentation, Release 1.8
And we add the translation through the Controller class using the next code
And we define the constants in English.ring and Arabic.ring
class selobjectsController from windowsControllerParent
oView = new selobjectsView {
ListObjects.setselectionmode(QAbstractItemView_MultiSelection)
win.setwindowmodality(2)
# Translation
win.setWindowTitle(T_FORMDESIGNER_SELOBJECTS_TITLE)
win.setLayoutDirection(T_LAYOUTDIRECTION)
labelobjects.setText(T_FORMDESIGNER_SELOBJECTS_OBJECTS)
btnSelect.setText(T_FORMDESIGNER_SELOBJECTS_SELECT)
btnClose.setText(T_FORMDESIGNER_SELOBJECTS_CLOSE)
}
64.3. Forms Translation 756
7. CHAPTER
SIXTYFIVE
SCOPE RULES FOR VARIABLES AND ATTRIBUTES
In this chapter we will learn about scope rules and how Ring find variables.
Also we will learn about conflicts and how to solve/avoid them.
The next information are important once you start developing large applications using Ring
These application may uses
• Global variables (Try to avoid them)
• Classes (Object-Oriented)
• braces { } to access objects
• Declarative Programming
• Natural Programming
65.1 Three Scopes
In Ring we have three scopes :-
1. Public/Global Scope - Each variable you define in the statements part (before functions and classes)
2. Object Scope - When you are inside an object (Inside class method or using { } to access the object )
3. Local Scope - Related to functions and methods
65.2 Defining Variables and Variables Access
1. Ring uses lexical scoping, i.e. the scope of the variable is based on where we defined the variable.
2. Inside braces { } when you access an object, You will change the current active object scope to this object scope
but you still can access the global scope and the local scope.
3. After the ‘Class’ keyword and the class name, when you write variable names to be defined as attributes, You
still can access the global scope.
In this region (class region - after the class name and before methods) we have
• Global Scope —-> The Global Scope
• Object Scope —-> The Object Scope
• Local Scope —-> The Object Scope
757
8. Ring Documentation, Release 1.8
Note: Since the local scope in the class region point also to the object scope in this region, we can use nested braces
and still have access to the object scope of the class through the local scope.
Tip: You can create windows and controls as attibutes by defining them in this region.
Tip: In the class region if you created objects and used braces {} to access them then using self.attribute inside braces
will use the class (not the object that you access) because you have access to the class through the local scope.
4. Function Parameters are automatically defined in the local scope.
65.3 How Ring find the variable?
1 - Search First in the Local Scope
if not found !
2 - Search in the Object Scope
if not found !
3 - Search in the public scope
if not found —-> Runtime Error
if found —-> Check if we can do optimization to avoid searching next time (Cache / Pointers for performance).
65.4 Using Object.Attribute
When we use object.attribute the search will be in the object attributes only.
I.e. no search will be done in the local scope or in the global scope for the object attribute.
Note: Using self.attribute will search for the first self before searching for attributes.
65.5 The Self Object
The self object is a reference to the current object that we can use from the class methods.
When we are inside class method and use Self we mean the object that will be created from this class.
Inside the class methods if we used Braces { } this will change the current object scope and self will be changed also
inside braces to reference the object that we access using Braces.
Inside the Class Region (after the class name and before any method) we have access to the object through the object
scope and the local scope also. In this region using Self will always be a reference to the class object. if we used
Braces to change the object scope then used Self inside Braces, Also self will be a reference to the class object (not
the object that we already access using braces) because in the class region we have :-
• Global Scope —> Global Scope
• Object Scope —> Object Scope
65.3. How Ring find the variable? 758
9. Ring Documentation, Release 1.8
• Local Scope —> Object Scope
And using Braces changes the object scope only (not the local scope) and when Ring search for variables it will search
in the Local Scope first so it will find self in the class that we are inside.
65.6 How Ring Define Variables and Attributes
Ring will use the variable name in the Assignment operation
1 - Search using the variable name
2 - If not found —> Avoid the runtime error and define the variable in the current scope
3 - If found —> Use the variable and don’t define anything in the current scope
• In the global region (before any function or class) the current scope is the global scope.
• In the class region (after the class name and before any method) the current scope is the object attributes.
• In Functions and methods the current scope is the local scope.
65.7 Conflict between Global Variables and Class Attributes
Look at this example:
name = "test"
o1 = new person
see o1
class person
name
address
phone
In the previous example we have a global variable called ‘name’ inside the class person.
when we use the variable ‘name’, Ring will start the search operation and will try to find it.
if found —> Use it
if not found —> Define new attribute
But the variable name is a global variable, so it will be found and used!
We will not have the attribute name! added to the object.
Solution (1) - Use the Main Function
func main
name = "test"
o1 = new person
see o1
class person
name
address
phone
Solution (2) - Use special mark for global variable names like $
65.6. How Ring Define Variables and Attributes 759
10. Ring Documentation, Release 1.8
$name = "test"
o1 = new person
see o1
class person
name
address
phone
Solution (3) - Use the AddAttribute() Method
name = "test"
o1 = new person
see o1
class person
AddAttribute(self,"name")
address
phone
Solution (4) - Use self before the attribute name
name = "test"
o1 = new person
see o1
class person
self.name
address
phone
So what is the best solution to this conflict?
1 - Use the $ Mark for global variables
2 - Optional : Try to avoid global variables and use the Main function
In practice i do both of them.
The other solution
• Use self before the attribute name or use AddAttribute()
65.8 Conflict between Class Attributes and Local Variables
This conflict may happen when we access the object using braces
Example:
func main
name = "nice"
o1 = new person {name="mahmoud" address="Egypt" phone = 000 }
see o1
class person
name
address
phone
65.8. Conflict between Class Attributes and Local Variables 760