Creating a non-draggable Spark TitleWindow container in Flex 4

The following example shows how you can create a non-draggable Spark TitleWindow container in Flex 4 by listening for the windowMoving event and calling the stopImmediatePropagation() and preventDefault() methods.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/01/19/creating-a-non-draggable-spark-titlewindow-container-in-flex-4/ -->
<s:Application name="Spark_TitleWindow_windowMoving_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Button id="btn"
                label="launch Spark TitleWindow"
                click="btn_clickHandler(event);" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.core.IFlexDisplayObject;
            import mx.events.CloseEvent;
            import mx.managers.PopUpManager;
            import spark.events.TitleWindowBoundsEvent;
 
            protected function btn_clickHandler(evt:MouseEvent):void {
                // Add and center the pop up window.
                PopUpManager.addPopUp(titleWin, this, true);
                PopUpManager.centerPopUp(titleWin);
            }
 
            protected function titleWin_closeHandler(evt:CloseEvent):void {
                // Remove the pop up window.
                PopUpManager.removePopUp(evt.currentTarget as IFlexDisplayObject);
            }
 
            protected function titleWin_windowMovingHandler(evt:TitleWindowBoundsEvent):void {
                // Before moving the TitleWindow container, check to see whether it should be draggable.
                if (!ch.selected) {
                    evt.stopImmediatePropagation();
                    evt.preventDefault();
                }
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:TitleWindow id="titleWin"
                title="Spark TitleWindow container"
                width="240"
                close="titleWin_closeHandler(event);"
                windowMoving="titleWin_windowMovingHandler(event);">
            <s:layout>
                <s:VerticalLayout paddingLeft="10" paddingRight="10"
                        paddingTop="10" paddingBottom="10" />
            </s:layout>
            <s:controlBarContent>
                <s:CheckBox id="ch"
                        label="Should I be draggable?"
                        selected="true" />
            </s:controlBarContent>
            <s:Label text="The quick brown fox jumps over the lazy dog."
                    width="100%" />
        </s:TitleWindow>
    </fx:Declarations>
 
</s:Application>

And a big thanks to my co-worker Kevin Lin for the following tip. You can also toggle a Spark TitleWindow container’s dragability by setting the moveArea skin part’s Boolean visible property, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/01/19/creating-a-non-draggable-spark-titlewindow-container-in-flex-4/ -->
<s:Application name="Spark_TitleWindow_moveArea_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Button id="btn"
                label="launch Spark TitleWindow"
                click="btn_clickHandler(event);" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.core.IFlexDisplayObject;
            import mx.events.CloseEvent;
            import mx.managers.PopUpManager;
            import spark.events.TitleWindowBoundsEvent;
 
            protected function btn_clickHandler(evt:MouseEvent):void {
                // Add and center the pop up window.
                PopUpManager.addPopUp(titleWin, this, true);
                PopUpManager.centerPopUp(titleWin);
            }
 
            protected function titleWin_closeHandler(evt:CloseEvent):void {
                // Remove the pop up window.
                PopUpManager.removePopUp(evt.currentTarget as IFlexDisplayObject);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:TitleWindow id="titleWin"
                title="Spark TitleWindow container"
                width="240"
                close="titleWin_closeHandler(event);"
                creationComplete="titleWin.moveArea.visible = ch.selected;">
            <s:layout>
                <s:VerticalLayout paddingLeft="10" paddingRight="10"
                        paddingTop="10" paddingBottom="10" />
            </s:layout>
            <s:controlBarContent>
                <s:CheckBox id="ch"
                        label="Should I be draggable?"
                        selected="true"
                        change="titleWin.moveArea.visible = ch.selected;" />
            </s:controlBarContent>
            <s:Label text="The quick brown fox jumps over the lazy dog."
                    width="100%" />
        </s:TitleWindow>
    </fx:Declarations>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.