Download documentation |
Today we have to find a way to save and load data and also link the variables A,B,...G to calculate expressions.
We add expressions and variables that will be loaded by default to calculator.gt. We will store the values of variables together with each expression. Once we select an expression, all variables will be changed.
<|data>
<_ = "" A = 0 B = 0 C = 0 D = 0. E = 0. F = 0. G = 0.>
<_ = "a*b" A = 10 B = 20 C = 0 D = 0. E = 0. F = 0. G = 0.>
<_ = "2*a*b" A = 3.1415 B = 20. C = 0 D = 0. E = 0. F = 0. G = 0.>
</data>
The first expression calculates the area of a rectangle and the second one calculates the circumference of a circle with the radius of B.
Let us add the identifiers of the main window and the expression list, as well as the string array for storing states, to global variables. We will use 8 strings for each expression (the expression string and 7 strings for variables).
uint dlg exprwnd
arr data of str
Let us start from the varset function. It takes strings from the fields for entering variables and converts then into the values of variables. register is a global array from expression.g where variables are stored. We also use the global variable dlg - it is the identifier of the main window. We will use the point or its absence to tell the type of a variable. If there is no point, the variable is a whole number.
func varset( uint num )
{
str stemp
getwintext( GetDlgItem( dlg, $IDC_VA + num ), stemp )
if stemp.findch( '.', 0 ) < *stemp
{
register[ num ].id = $NUM_DOUBLE
register[ num ].dval = str2double( stemp )
}
else
{
register[ num ].id = $NUM_LONG
register[ num ].lval = str2long( stemp )
}
}
Below you see the loadcur function that loads the expression selected from the drop-down list and its variables.
uint i
uint cur = combo_getcurrent( exprwnd ) << 3
setwintext( exprwnd, data[ cur ] )
fornum i = 0, 7
{
setwintext( GetDlgItem( dlg, $IDC_VA + i ), data[ cur + i + 1 ] )
varset( i )
}
There is one more function to be loaded - varload. It will be called each time the program is started and it will be responsible for the primary initialization of the drop-down expression list and variables.
gtitems gtis
str stemp
uint i
fornum i, *data >> 3
{
combo_add( exprwnd, data[ i << 3 ] )
}
combo_setcur( exprwnd, 0 )
loadcur()
Let us define the gt2arr function that will fill the data array of strings with GT data. We will call this function at the very beginning of the program.
gtitems gtis
str stemp
uint i count
foreach cur, gtapp.find("data").items( gtis )
{
count = *data
data.expand( 8 )
cur.get( data[ count ] )
fornum i = 0, 7
{
cur.getattrib( "".appendch( 'A' + i ), data[ count + i + 1 ])
}
}
There are some minor changes in the main handler left to be made. Let us add loading data during the initialization.
dlg = wnd
exprwnd = GetDlgItem( dlg, $IDC_EXPR )
varload()
Let us add changing the expression and variables when some other item is selected from the drop-down list.
case $IDC_EXPR
{
if codedlg == $CBN_EDITCHANGE : calcexpr()
if codedlg == $CBN_SELCHANGE
{
loadcur()
calcexpr()
}
}
And finally we will recalculate the expression each time variables in the edit field are modified.
if id >= $IDC_VA && id < $IDC_VA + 7 && codedlg == $EN_CHANGE
{
varset( id - $IDC_VA )
calcexpr()
}
See also
![]() |
![]() Copyright © 2004-2005 Gentee, Inc. All rights reserved. ![]() |