LiveCode
  • Why LiveCode?
  • Customers
  • Resources
  • Education
  • Consulting
  • Pricing
  • Blog
  • Login
  • Try LiveCode
Back to
  • slides
  • tutorial
Resources

Saving and Loading Widgets

Topic Progress:
← Back to Lesson

Saving and loading widgets

Creating, saving and loading widgets

In Lesson 3 we looked at the core handlers but so far we have only implemented the OnPaint and OnCreate handlers. In this lesson we will implement the OnSave and OnLoad handlers.

  • OnSave: Sent when your widget is about to be destroyed and enables the widget to save data set on the widget.
  • OnLoad: Sent when your widget is created and enables the widget to retrieve data saved on the widget.

Saving and loading the widget state

We want to be able to save and load the widget’s properties.

If the OnSave and OnLoad handlers are not included then every time you open a saved stack that includes a widget, the widget will lose its property data and the properties will be set to the default values.

When your widget is saved you are sent an OnSave message. It returns an array which you can fill with whatever widget data you have. LiveCode saves this data along with instances of the widget in the stack file.

This same array will be returned to you as a parameter to the the OnLoad message, which is sent when the widget is next opened and can be used to recreate the widget with its saved properties.

The OnSave handler

First we write the OnSave handler, which saves the widget’s properties when a stack containing a widget is saved.

Add the OnSave handler to the widget code. The rProperties array is an out parameter meaning  the value of the array is copied back to the caller when the handler completes.

Add an element to the rProperties array for each property

  • key: property name
  • value: property value
public handler OnSave(out rProperties as Array)
 put mContent into rProperties["content"]
 put mRotation into rProperties["rotation"]
end handler

Note: LiveCode properties are automatically saved so don’t need to be explicitly stored in the array.

The OnLoad Handler

Next we write the OnLoad handler, which loads the widget’s properties when a stack containing a widget is opened:

Add the OnLoad handler to the widget code.

The array of saved properties is passed in as a parameter, and can be used to set the values of the private instance variables to the stored property values.

  • Set the value of the content property to the value of the content element in the properties array.
  • Set the value of the rotation property to the value of the rotation element in the properties array.
public handler OnLoad(in pProperties as Array)
 put pProperties["content"] into mContent
 put pProperties["rotation"] into mRotation
end handler

Testing Saving and Loading a widget

In order to test saving and loading a widget we have to install the widget.

  • Open the Extension Builder from the Tools Palette
  • Load the lcb file
  • Uninstall the previous widget, if necessary
  • Click Install
  • Select the icons(found under the Resources tab)
  • The widget will appear in the Tools Palette
  • Create a new stack
  • Drag on a Rotated Text Widget
  • Set some properties
  • Save the stack
  • Close LiveCode
  • Reopen the stack

The widget should appear with its properties set to the saved state.

stack

← Previous Topic

← Previous Topic Next Topic →
  •  
  •  
  • (0)
  •  

Expand All | Collapse All
Lessons Status
1 - Hello World Library
  • Introduction
  • Installing a Text Editor
  • Creating an LCB library
  • LCB library definition
  • LCB handler definitions
  • Compiling and testing a library in LiveCode
  • LCB metadata
  • Installing an LCB Library
  • Packaging and sharing an LCB library
  • Including an LCB library in a standalone
  • Conclusion
2 - Extended Hello World Library
  • Introduction
  • Explicit Types
  • LCB Lists and Arrays
  • Type Conversion between LCB and LiveCode Script
  • Using Explicit Typing in the Library
  • Passing Parameters to the Library
  • Using Lists in the Library
  • Documenting the Library
  • Browsing the Documentation
  • Conclusion
3 - Rotated Text Widget
  • Introduction
  • Defining a Widget
  • LCB Module Level Variables
  • Widget Properties
  • Widget Handlers
  • LCB Canvas Operations
  • Compiling and Testing the Widget
  • Installing the Widget and adding it to a Stack
  • Including the Widget in a Standalone
  • Conclusion
4 - Extended Rotated Text Widget
  • Introduction
  • Integrating Properties
  • Accessing LiveCode Control Properties
  • Handling Mouse Events
  • Saving and Loading Widgets
  • Read Only Properties
  • Documenting the Widget
  • Conclusion
5 - Pie Chart Widget
  • Introduction
  • Planning the Widget
  • Widget Properties
  • The OnCreate Handler
  • The OnPaint Handler
  • Responding to Mouse Events
  • Saving and Loading the Widget
  • Installing and Including the Widget in a Standalone
  • Documenting the Widget
  • Conclusion
6 - Modifying the Line Graph Widget
  • Introduction
  • Duplicating the Line Graph Widget
  • Additional Color Properties
  • Filling the area below the line
  • Adding graph marker mouse events
  • Conclusion
7 - Additional Resources
  • Extending LiveCode Guide
  • LiveCode Builder Language Reference
  • LiveCode Builder Style Guide
  • LiveCode Documentation Format
 

Icons

Icons

LCB Source

LCB Source
 

Login

LiveCode

  • Why LiveCode?
  • Pricing
  • Customer Stories
  • Extensions
  • LiveCode in Education
  • LiveCode in Business

Looking for LiveCode FileMaker?

LiveCode for FM

Resources

  • Docs
  • API (Language Dictionary)
  • Lessons
  • Sample Stacks
  • Forums
  • Stackoverflow
  • User Groups
  • Support
  • Directory
  • LiveCode Services

About

  • About
  • Meet The Team
  • Careers at LiveCode
  • Contact us

Recent Posts

  • LiveCode Classic 10.0.1 Stable and 10.0.2-rc-1 Released
  • Create Progress – the Three D’s
  • LiveCode Create: Closing out the year with big steps forward
  • Create dp-5: Unified Engines and Shared Extensions
  • 3 Great New LiveCode Releases
Sitemap
Terms Privacy Policy EULA
Brought to you by LiveCode Ltd, Registered in Scotland, No. SC200728
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok