XWidgetSoft Forum
https://www.bbs.xwidget.com/

saving and getting user defined variables
https://www.bbs.xwidget.com/viewtopic.php?f=10&t=2316
Page 1 of 1

Author:  qiancang [ November 23rd, 2012, 6:41 am ]
Post subject:  saving and getting user defined variables

Some widgets support user defined settings, such as slider shadow style by 9yomo . We should save these user defined settings in external file and reload them when XWidget restarts or widget reloads.

There are 2 ways to save user defined settings, one way is setvalue() and getvalue() functions , the other way is setinivalue() and getinivalue().
Before starting learning them, please read post managing themes. It’s helpful.

What’s the difference between these 2 ways?
  • The function setvalue() will save variables in the theme file "theme.xwt". If one widget is closed, the variables saved before will be deleted. Function Getvalue() could not get these variables again.
  • The function setinivalue() will save variables in an external ini file. Even if you close the widget, these variables would not be lost. Function Getinivalue() is able to get them again every time you load the widget.

The knowledge of form of ini file is required. The form is shown below.
Attachment:
iniform.jpg
iniform.jpg [ 34.31 KiB | Viewed 12742 times ]


Setvalue() & Getvalue()
Code:
Setvalue("keyName","keyValue");
Savevalue();

theme.xwt file is a kind of ini file indeed. Setvalue() saves variable from widget to memory and savevalue() saves variable from memory to disk. They must be used together.

Code:
Getvalue("keyName","defaultValue");

Getvalue() is written in the function widgetOnLoad() generally. If you write
Code:
var num=Getvalue("uptime",18);

and there is not a keyname called "uptime" in theme.xwt, Getvalue() returns 18. Hence, num=18.

Setinivalue & Getinivalue
Code:
Setinivalue("inifile path","section","keyName","keyValue");

"inifile path" is the full path of the ini file to which you want to save variable, such as
Code:
"C:\\Program Files\\XWidget\\uptime.ini"

or
Code:
widgetpath+"uptime.ini"

Please use double '\' in the path. widgetpath is a variable provided by XWidget that presents the path of the widget.

Code:
Getinivalue("inifile path","section","keyName","defaultValue");


Examples
  • Saving font.fill.color
    Code:
    function text1OnClick()
    {
      Setvalue("fontcolor",text1.font.fill.color);
      Savevalue();
    }

    function widgetOnLoad()
    {
      text1.font.fill.color=Getvalue("fontcolor",rgba(0,0,0,255));
    }


    Code:
    function text1OnClick()
    {
      Setinivalue(widgetpath+"colors.ini","colors","text1fontcolor",text1.font.fill.color);
      Setinivalue(widgetpath+"colors.ini","colors","text2fontcolor",text2.font.fill.color);
    }

    function widgetOnLoad()
    {
      text1.font.fill.color=Getinivalue(widgetpath+"colors.ini","colors","text1fontcolor",rgba(0,0,0,255));
      text2.font.fill.color=Getinivalue(widgetpath+"colors.ini","colors","text2fontcolor",rgba(255,0,0,255));
    }

  • Saving image src
    Code:
    function image1OnClick()
    {
      Setvalue("src",image1.src);
      Savevalue();
    }

    function widgetOnLoad()
    {
      image1.src=Getvalue("src",widgetpath+"black.png");
    }


    Code:
    function image1OnClick()
    {
      Setinivalue("D:\\mydocumets\\src.ini","src","image1src",image1.src);
      Setinivalue("D:\\mydocumets\\src.ini","src","image2src",image2.src);
    }

    function widgetOnLoad()
    {
      iamge1.src=Getinivalue("D:\\mydocumets\\src.ini","src","image1src","D:\\myimages\\black.png");
      iamge2.src=Getinivalue("D:\\mydocumets\\src.ini","src","image2src","D:\\myimages\\white.jpg");
    }

  • Saving integer variable
    Code:
    var num=18;
    function menuitem1OnClick()
    {
      Setvalue("number",num);
      Setvalue("number2",100);
      Savevalue();
    }

    function widgetOnLoad()
    {
      num=parseInt(Getvalue("number",10));
    }

    Sometimes you need to converte data from string to other types since all the data will be saved and gotten as string.

Author:  Armando [ November 23rd, 2012, 9:50 am ]
Post subject:  Re: saving and getting user defined variables

We will waiting... ;)

Author:  meme [ April 20th, 2013, 3:00 am ]
Post subject:  Re: saving and getting user defined variables

Thank you qiancang for this valuable information :D
Could you please add a description for the function SaveAsDefIni() :?:

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/