Showing posts with label Windows Sidebar. Show all posts
Showing posts with label Windows Sidebar. Show all posts
Sunday, 16 March 2014
Developing a Gadget for Windows Sidebar Part 3



The last of three overviews that describe how to create a basic gadget for the Windows Sidebar. In this overview, we explain the difference between Settings (or Options as specified in the gadget context menu) and Flyout functionality and demonstrate how to incorporate both into a gadget.
  • Introduction
  • The Settings Dialog
  • The Flyout
  • For Further Reference

Introduction

The gadget object model defines two user interface (UI) extensions to the basic gadget UI. The gadget's Settings dialog (System.Gadget.Settings) enables a user to make configuration changes to a gadget while the gadget's Flyout (System.Gadget.Flyout) enables a user to view additional detail or information about a gadget UI.

The Settings Dialog


Settings functionality is exposed by the System.Gadget.Settings and System.Gadget.Settings.ClosingEvent scripting elements. The Settings dialog possesses its own Document Object Model (DOM) and requires its own HTML file (specified by assigning the path of an HTML file to the settingsUI property), Microsoft JScript, and other supporting files. Assigning a value to the settingsUI property results in the display of a "wrench" icon which appears when the mouse pointer hovers over the gadget or the gadget has focus. Clicking this icon or selecting Options from the gadget context menu will display the Settings dialog.



The following example demonstrates how to use the settingsUI property to enable the gadget Settings dialog box.
// Enable the gadget settings functionality. 
System.Gadget.settingsUI = "Example.html";

The primary purpose of the Settings dialog is to allow the user to select and modify gadget configuration values (or settings). These values are persistent for the entire session of a particular gadget and unique for each gadget instance. For example, a stock tracking gadget can enable a user to specify a data provider and the stock information display preferences in the gadget and subsequent modification in the Settings dialog.
A Settings value is stored by using write or writeString with a key/value pair. A Settings value is retrieved by using read or readString with the key declared when the value was stored. There is a 1024 character limit for Settings keys and a 2048 character limit for Settings values; values longer than these limits will be truncated.
Note  To limit type conversion errors, the use of writeString and readString is recommended.


The following example demonstrates how to store a Settings value when the user clicks OK in the Settings dialog.
// Delegate for the settings closing event. 
System.Gadget.onSettingsClosing = SettingsClosing;

// --------------------------------------------------------------------
// Handle the Settings dialog closing event.
// Parameters:
// event - event arguments.
// --------------------------------------------------------------------
function SettingsClosing(event)
{
    // Save the settings if the user clicked OK.
    if (event.closeAction == event.Action.commit)
    {
        System.Gadget.Settings.write(
            "settingsSelectionIndex", selUserEntry.selectedIndex);
    }
    // Allow the Settings dialog to close.
    event.cancel = false;
}
The following example demonstrates how to read values from controls in the Settings dialog.
var userEntry = "";

System.Gadget.onSettingsClosed = SettingsClosed;

// --------------------------------------------------------------------
// Handle the Settings dialog closed event.
// event = System.Gadget.Settings.ClosingEvent argument.
// --------------------------------------------------------------------
function SettingsClosed(event)
{
    // User hits OK on the settings page.
    if (event.closeAction == event.Action.commit)
    {
        userEntry = 
            System.Gadget.Settings.readString("settingsUserEntry");
        SetContentText(userEntry);
    }
    // User hits Cancel on the settings page.
    else if (event.closeAction == event.Action.cancel)
    {
        SetContentText("Cancelled");
    }
}
Note  A Settings dialog is modal and does not disappear if it or the parent gadget loses focus. The user must select Ok or Cancel to dismiss the dialog.

The Flyout

Flyout functionality is exposed by the System.Gadget.Flyout scripting element. Flyouts also require their own HTML and JScript files and, as such, possess their own DOM. Communication between the Flyout and the main gadget can be accomplished through the use of the parentWindow property of the System.Gadget.document and System.Gadget.Flyout.document objects. The Flyout file is specified by setting System.Gadget.Flyout.file to the path of an HTML file.
Note   Since a Flyout can disappear at any time, unpredictable behavior or errors may result from attempting to communicate with a Flyout from the main gadget.
The following example demonstrates how to set the gadget Flyout UI from the gadget script file and listen for the Flyout events.
var oGadgetDocument = System.Gadget.document;
System.Gadget.Flyout.onShow = showFlyout;
System.Gadget.Flyout.onHide = hideFlyout;

// --------------------------------------------------------------------
// Initialize the gadget.
// --------------------------------------------------------------------
function Init()
{
    // Specify the Flyout root.
    System.Gadget.Flyout.file = "example.html";
    
    // Initialize the Flyout state display.
    if (!System.Gadget.Flyout.show)
    {
        sFlyoutFeedback.innerText = "Flyout hidden.";
    }
}

// --------------------------------------------------------------------
// Display the Flyout state in the gadget.
// --------------------------------------------------------------------
function showFlyout()
{
    oGadgetDocument.getElementById("strFlyoutFeedback").innerText = "Flyout visible.";
}

// --------------------------------------------------------------------
// Hide the flyout and display the Flyout state in the gadget.
// --------------------------------------------------------------------
function hideFlyout()
{
    oGadgetDocument.getElementById("strFlyoutFeedback").innerText = "Flyout hidden.";
    System.Gadget.Flyout.show = false;
}

A Flyout is typically spawned on an event in the main gadget UI such as a mouseover or click; setting show to true displays the Flyout and false hides the Flyout. This state should be queried before any communication is attempted. Flyouts can be used to display anything but are generally used to display more detail or information about the event target.
The Flyout location is determined by the user's display, the position of the gadget, the size of the Flyout (which has no intrinsic limit), and the location of the Sidebar should the gadget be docked. For example, the Flyout could be displayed to the left or right, above or below the gadget.
The following image shows a gadget Flyout that displays stock ticker details.

The following image shows a gadget Flyout that allows a user to select stock symbols of interest.


Note  A Flyout closes when it loses focus. As a result, only one Flyout can be displayed at any time.

Similar posts : 

Developing a Gadget for Windows Sidebar Part 2

In this overview, we demonstrate how to declare a background image and add some simple text and graphics to a gadget using the three presentation elements in the Sidebar g namespace (g:background, g:image, and g:text) and the native Sidebar image protocol (gimage).
  • Introduction
  • Background Images and the G:BACKGROUND Element
  • The G:TEXT and G:IMAGE Elements
  • The GIMAGE Protocol
    • Sizing

Introduction

The Sidebar presentation elements expose their functionality through qualified HTML elements in the Sidebar g namespace and the gadget presentation APIs. These elements, along with all standard HTML elements, are exposed through the gadget Document Object Model (DOM) and are accessible from gadget script.
The Sidebar supports JPEG, bitmap (BMP), Graphics Interchange Format (GIF), and Portable Network Graphics (PNG) image types. Only PNG supports alpha channel transparency.
Note   The Sidebar presentation elements are not available in standard HTML documents and do not render in external browsers such as Windows Internet Explorer.

Background Images and the G:BACKGROUND Element

There are a number of methods that can be used to declare a gadget background element and specify its image source. Generally, the background object and its image source are declared in the gadget HTML file using the g:background element:
<body>
    <g:background id="imgBackground" src="images/background.png">
        <span id="gadgetContent">Hello World!</span>
    </g:background>
</body>
An alternative is to declare the g:background as previously described, but specify the image source in Microsoft JScript:
<html>
    <head>
        <title>Hello World</title>
        <script type="text/jscript" language="jscript">
            function init()
            {
                var oBackground = document.getElementById("imgBackground");
                oBackground.src = "url(images/background.png)";
            }
        </script>
    </head>
    
    <body onload="init()">
        <g:background id="imgBackground">
            <span id="gadgetContent">Hello World!</span>
        </g:background>
    </body>
</html>
Caution  When using background images with alpha channel transparency the following two methods can result in unpredictable rendering, such as transparent pixels replaced with magenta, and limitations in functionality. As such, they are presented here for information purposes only and are not recommended.
Important  For Windows 7, when the new <autoscaleDPI> element of the gadget manifest is set to true (for high-DPI support) and the following methods are used, rendering issues can increase significantly.
The following example uses the background property to declare the gadget background image:
<html>
    <head>
        <title>Hello World</title>
        <script type="text/jscript" language="jscript">
        function init()
        {
            System.Gadget.background = "images\\background.png";
        }
        </script>
    </head>
    
    <body onload="init()">
        <g:background id="imgBackground">
            <span id="gadgetContent">Hello World!</span>
        </g:background>
    </body>
</html>
A similar method uses Cascading Style Sheets (CSS) to update the backgroundImage Property as shown in the following example:
<html>
    <head>
        <title>Hello World</title>
        <script type="text/jscript" language="jscript">
        function init()
        {
            with(document.body.style)
                backgroundImage = "images\\background.png"; 
        }
        </script>
    </head>
    
    <body onload="init()">
        <g:background id="imgBackground">
            <span id="gadgetContent">Hello World!</span>
        </g:background>
    </body>
</html>
Finally, just like a standard Web page, the gadget background can simply be declared as an attribute of the <body> tag and modified with CSS. However, this does not expose the background as a g:background scripting element.
<html>
    <head>
        <style>
            body{width:120;height:160;font:8 pt Arial;color:white;
            filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr="#000000",
            EndColorStr="#0000FF")}
        </style>
    </head>
    <body bgcolor="red" background="background.jpg">
    ...
    </body>
</html>

When one background image needs to be swapped for another of different size, such as when a gadget is docked or undocked, it is recommended that the width of the background element be set to 0. This forces the platform to recalculate the size of the image and refresh appropriately.
See the onUndock or onDock topics for an example.

The G:TEXT and G:IMAGE Elements

The g:text and g:image elements expose gadget text and image functionality respectively.
Note   The g:text and g:image elements can be declared in the gadget HTML file but it is highly recommended that you use the addImageObject and addTextObject methods from JScript instead. This will ensure the element is exposed through the gadget DOM.
The following example demonstrates how to add images and text to the background layer of a gadget. These objects will appear at the top of the z-order for the background layer, but below any user interface (UI) controls.
Caution  Other than g:background, avoid images that render to the absolute edges of a gadget. In high-DPI, rounding errors can cause a magenta fringe around the border of the gadget.
// Initialize the gImage object
// DEFAULT_IMG_PATH: the default image path.
function initImage()
{
    // Check if the user has already selected an image file to use. If not use default.
    if (System.Gadget.Settings.read("imageFile") == "")
    {
        System.Gadget.Settings.write("imageFile", DEFAULT_IMG_PATH);
    }
    imageFile = System.Gadget.Settings.read("imageFile");
    
    // Create the image object
    oImage = document.getElementById("background").addImageObject("", 0, 0);
}

// Initialize the gText object
function initText()
{
    oText = document.getElementById("background").addTextObject("", "Verdana", 10, "blue", 0, 0);
}

function showImage()
{
    oImage.src = System.Gadget.Settings.read("imageFile");
}

function showText(newValue)
{
    oText.value = newValue;
}
g:image and g:text objects can also be added to the gadget DOM by declaring them in the gadget HTML file:
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
    <link href="css/Graphic.css" rel="stylesheet" type="text/css" />
    <script language="javascript" src="js/graphic.js" type="text/javascript"></script>
</head>
<body>
<g:background 
id="imgBackground" 
src="images\background.png">
<g:text id="txtTest" blur="20">test</g:text>
<g:image id="imgTest" src="images\aerologo.png"/></div>
</g:background>
</body>
</html>
Note   This method does not expose the objects as g:image or g:text elements. You can locate and assign the objects using the document.getElementById("...") method, but the g:image or g:text methods and properties are not available.

The GIMAGE Protocol

This protocol is useful for adding images to the gadget DOM more efficiently than the standard HTML <img> tag. This efficiency results from improved thumbnail handling and image caching (it will attempt to use thumbnails from the Windows cache if the requested size is smaller than 256 pixels by 256 pixels) when compared with requesting an image using the file:// or http:// protocols. An added benefit of the gimage protocol is that any file other than a standard image file can be specified as a source, and the icon associated with that file's type is displayed.
Note   When the gimage protocol is specified, the requested file must be on the local machine and must not contain a server reference.
Despite its name, the gimage protocol is unrelated to the g:image element. The image, or associated icon, is returned as a bytestream and, as such, is not exposed to gadget script as a g:image element. However, the <img> object is still a member of the gadget DOM and is exposed through standard JScript members.
function findGIMAGE()
{
    var oGIMAGE = document.getElementById("imgGIMAGE");
    
    // Get a standard JScript <img> object property.
    var srcValue = oGIMAGE.src;
    
    // Undefined G:IMAGE property.
    var opacityValue = oGIMAGE.opacity;
}

...

<img id="imgGIMAGE" 
src="gimage:///C:\Users\user\AppData\Local\Microsoft\Windows Sidebar\Gadgets\SDK_Graphic.gadget\js\graphic.js" />

Sizing

As stated previously, one of the benefits of the gimage protocol is enhanced thumbnail handling and image sizing. Typically, you would supply the image height and width as a querystring appended to the src attribute value or as an inline style.
// --------------------------------------------------------------------
// Add an image to the gadget DOM using the gimage protocol.
// --------------------------------------------------------------------
function addGIMAGE()
{
    var heightWidthLoad = "?width=25&height=25";
    var oImage = document.createElement("img");
    oImage.src = "gimage:///" + System.Gadget.path + "\\images\\aerologo.png" + heightWidthLoad;
    oImage.id = "imgGIMAGEx";
    document.body.appendChild(oImage);
}

// --------------------------------------------------------------------
// Switch an image using the gimage protocol.
// --------------------------------------------------------------------
function switchGIMAGE(file)
{
    // Specify the height, width, and interpolation method.
    imgGIMAGE.style.height = 25;
    imgGIMAGE.style.width = 25;
    imgGIMAGE.style.msInterpolationMode = "bicubic";
    imgGIMAGE.src = "gimage:///" + System.Gadget.path + "\\images\\aerologo.png";
}

<!--
Specify gimage details from script.
-->
<img id="imgGIMAGE" src="images/blank.png" height="0" width="0" />

If only one of the height and width is specified, the image is scaled proportionally; if neither the height or width is specified, the image is scaled proportionally to 120 pixels by 120 pixels. In either case, any unfilled areas are rendered as transparent.


Similar posts : 

Developing a Gadget for Windows Sidebar Part 1

The first of three overviews that describe how to create a basic gadget for the Windows Sidebar. In this overview, we demonstrate a simple "Hello World" gadget and the steps required to install and display it in the Sidebar.
  • Introduction
  • The Files
  • The Steps
  • The Example
  • For Further Reference

Introduction

Gadgets are lightweight HTML and script-based applications that provide the abillity to derive and present information or functionality from a variety of sources, such as local applications and controls, or websites and services. Developers with experience authoring webpages will find the process of creating a gadget very familiar.

The Files

A basic gadget consists of two files:
  1. Gadget.xml - The manifest, an XML file that contains general configuration and presentation information for the gadget.
  2. name.html - An HTML file, where name is specified in the <name> tag of the associated gadget manifest, that provides the shell for the gadget UI and contains the core functionality for the gadget.
Important  
It is highly recommended that all gadget HTML and script files be saved with UTF-8 character encoding.
The following steps can be taken to ensure the encoding of these files:
  1. Open the file in Notepad.
  2. On the File menu, click Save as...
  3. In the Save as dialog box, confirm that the value in the Encoding drop-down is UTF-8.
If the value in the Encoding drop-down is not UTF-8:
  1. In the Encoding drop-down, select UTF-8.
  2. Click Save to overwrite the existing file.
Repeat this process for all gadget HTML and script files.
A more robust gadget implementation may require other files not detailed here. For the purposes of this overview, however, the discussion is limited to these two core files.

The Steps

In general, the steps for creating a gadget are:
Create a development folder to contain the gadget files. It is generally good practice to give the development folder the same name as the gadget it hosts, with the added extension of .gadget. For example, if your gadget's name is "Test" then the development folder should be named "Test.gadget". This reduces naming confusion later when it comes time to install the gadget. However, the development folder can have any name you wish.
Similarly, the development folder can be located anywhere. However, during development and testing it is typically more efficient to place the folder in one of the system folders associated with the Sidebar:
  • %USERPROFILE%\AppData\Local\Microsoft\Windows Sidebar\Gadgets (for user gadgets)
  • %SYSTEM_ROOT%\Program Files\Windows Sidebar\Gadgets (for global gadgets)
The following image shows a gadget development folder in the %USER_DATA%\Local\Microsoft\Windows Sidebar\Gadgets folder.

  1. Create the manifest file and save it to the development folder. For more information on the gadget manifest, see Gadgets for Windows Sidebar Manifest.
    <?xml version="1.0" encoding="utf-8" ?>
    <gadget>
      <name>SDK Shell</name>
      <version>1.0.0.0</version>
      <hosts>
        <host name="sidebar">
          <base type="HTML" apiVersion="1.0.0" src="Shell.html" />
          <permissions>Full</permissions>
          <platform minPlatformVersion="1.0" />
        </host>
      </hosts>
    </gadget>
    
    
    
  2. Create the core .html file and save it to the development folder.
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
        </head>
        
        <body>
            <div id="gadgetContent">
            </div>
        </body>
    </html>
    
    
  3. Install the gadget, if necessary. Depending on where you created your development folder, you may need to copy the folder or its content to one of the two previously identified Sidebar system folders. Alternatively, you may want to package the gadget for general distribution and test the gadget installation process. For more information on installing and updating a gadget, see Gadgets for Windows Sidebar Updating and Refreshing.
  4. Test the gadget and make revisions as necessary.

The Example

The following is a step-by-step example for creating a simple "Hello World" gadget.
  1. To open the Sidebar, click the Start button, point to All Programs, then to Accessories, and then click Windows Sidebar. You can also click the Start button, click Run..., and then type "sidebar" in the Open text field and press ENTER.
  2. Locate and open your gadgets folder. Click the Start button, and then click Run.... In the Open text box, type: %USERPROFILE%\AppData\Local\Microsoft\Windows Sidebar\Gadgets
  3. In your Gadgets folder, create a new folder named HelloWorld.gadget.
  4. Copy and paste the following code into Notepad or a similar editor that allows you to create an HTML file. Name the file HelloWorld.html, and save it in your HelloWorld.gadget folder.
    Important  If font information is specified for the gadget UI in HTML or Cascading Style Sheets (CSS), the font size must be included and it must be an absolute measure in pixels.
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=Unicode" />
            <title>Hello World</title>
            <style type="text/css">
            body
            {
                margin: 0;
                width: 130px;
                height: 75px;
                font-family: verdana;
                font-weight: bold;
                font-size: 20px;
            }
            #gadgetContent
            {
                margin-top: 20px;
                width: 130px;
                vertical-align: middle;
                text-align: center;
                overflow: hidden;
            }
            </style>
            <script type="text/jscript" language="jscript">
                // Initialize the gadget.
                function init()
                {
                    var oBackground = document.getElementById("imgBackground");
                    oBackground.src = "url(images/background.png)";
                }
            </script>
        </head>
     
        <body onload="init()">
            <g:background id="imgBackground">
                <span id="gadgetContent">Hello World!</span>
            </g:background>
        </body>
    </html>
    
    
  5. Create a new folder called images under the HelloWorld.gadget folder.
  6. Create a new background image at least 130 pixels wide and 75 pixels high and save it to the images folder.
  7. Create the gadget manifest by copying and pasting the following code into a new file. Save this file with the file name gadget.xml and UTF-8 encoding.
    <?xml version="1.0" encoding="utf-8" ?>
    <gadget>
      <name>SDK Hello World</name>
      <version>1.0.0.0</version>
      <author name="Microsoft">
        <info url="msdn.microsoft.com" />
      </author>
      <copyright>&#169; Microsoft Corporation.</copyright>
      <description>"HelloWorld" Sidebar gadget sample.</description>
      <hosts>
        <host name="sidebar">
          <base type="HTML" apiVersion="1.0.0" src="HelloWorld.html" />
          <permissions>Full</permissions>
          <platform minPlatformVersion="1.0" />
        </host>
      </hosts>
    </gadget>
    
    
    
  8. Click the "+" symbol at the top of the Sidebar to display the Gadget Gallery.


    9. In the Gadget Gallery, the "SDK Hello World" gadget should be visible.
    10. To install the gadget in the Sidebar, double-click the icon for the "SDK Hello World" gadget or drag and drop it to the Sidebar.
    Note  
    Unlike a standard Windows icon, a gadget icon is nothing more than a Web-based image file (.gif, .jpg, or .png). The image can be created using Microsoft Paint or similar image editing tool. If a custom icon isn't specified in the manifiest, Sidebar provides a generic icon for the gadget.
    When creating custom icons, it is recommended that they be 64 pixels wide by 64 pixels high. The gadget picker reserves a space of that size and resizes the icon accordingly. 

    Similar posts :