Flex 4.5 - Custom NativeWindow and problems - apache-flex

I have a class FlexNativeWindow who extends to NativeWindow.
I use this method to create new window on my AIR app.
All works well but some keyboard interraction is not availaible.
For exemple focus arround textinput is not visible.
Key down and up don't work on DropDownList.
TabOrder doesn't work well!
Can you explain Why? Because I'm very desapointed!
Code to create the new window
var wdetcorr:wDetailCorrespondant = new wDetailCorrespondant();
wdetcorr.monIdCorresp = correspDG.selectedItem.crIndex;
var wOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
wOptions.systemChrome = NativeWindowSystemChrome.NONE;
wOptions.transparent = false;
var fnwDetailPatient:FlexNativeWindow = new FlexNativeWindow(wdetcorr, wOptions);
fnwDetailPatient.active();
Code about my custom NativeWindow
package fr.int.ui.windowSkin
{
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import mx.core.IUIComponent;
import mx.core.IVisualElement;
import mx.core.IVisualElementContainer;
import mx.core.UIComponent;
import mx.events.*;
import mx.managers.WindowedSystemManager;
[Event(name="creationComplete", type="mx.events.FlexEvent")]
public class FlexNativeWindow extends NativeWindow implements IFlexNativeWindow
{
private var _systemManager:WindowedSystemManager;
private var _content:UIComponent;
private var _window:IVisualElementContainer;
public function FlexNativeWindow(window:IVisualElementContainer, initOptions:NativeWindowInitOptions = null)
{
super(initOptions);
_window = window;
addEventListener(Event.ACTIVATE, windowActivateHandler);
}
public function addElement(control:IVisualElement):void
{
_window.addElement(control);
}
public function removeElement(control:IVisualElement):void
{
_window.removeElement(control);
}
private function windowActivateHandler(event:Event):void
{
event.preventDefault();
event.stopImmediatePropagation();
removeEventListener(Event.ACTIVATE, windowActivateHandler);
if (stage)
{
if (!_systemManager)
_systemManager = new WindowedSystemManager(IUIComponent(_window));
stage.addChild(_systemManager);
dispatchEvent(new FlexEvent(FlexEvent.CREATION_COMPLETE));
stage.addEventListener(Event.RESIZE, windowResizeHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
}
}
private function keyDownListener (e:KeyboardEvent):void {
if (e.keyCode == Keyboard.ESCAPE) {
stage.nativeWindow.dispatchEvent(new Event("myEventClose", true));
stage.nativeWindow.close();
}
}
private function windowResizeHandler(event:Event):void
{
// prise en compte de la valeur mini
UIComponent(_window).height = stage.stageHeight;
UIComponent(_window).width = stage.stageWidth;
}
}
}

You should implement IFocusManagerContainer in FlexNativeWindow to fix the focus issues.
Here is how to do it in Flash Builder:
Press Ctrl + Shift + T and type in IFocusManagerContainer
Open corresponding file
Implement all declared methods in your class FlexNativeWindow
P.S: However this approach can fail because flex components should be placed inside flex container while NativeWindow is a native class, not a Flex SDK class. I suggest you to use Window class that is a Flex container and already implements IFocusManagerContainer.

Related

how to load fonts in flex 4.6 windowed application

I have a window application that i have developed in flex sdk 4.6 . I have the same version in web based application. In this application i am loading external swf font file. In web application everything is working fine but font is not loaded in window application.
Below is the code for loading the fonts..
package com
{
import adobe.utils.CustomActions;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.text.Font;
import injectors.CentralInjector;
import mx.core.FontAsset;
public class LF extends EventDispatcher
{
public function LF()
{
}
public function avaliable(style:String):Boolean{
var embeddedFonts:Array = Font.enumerateFonts(false);
for(var i:Number = 0; i < embeddedFonts.length; i++){
var item:Font = embeddedFonts[i];
if(item.fontStyle==style){
return true;
}
}
return false;
}
public function load(url:String):void{
//=====url is http://www.mydomain.com/font1.swf=================
CustomWaitAlert.show(CustomWaitAlert.WAIT,null,"Loading fonts...");
var loader:Loader = new Loader();
var loaderInfo:LoaderInfo = loader.contentLoaderInfo;
loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioerror);
loaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(url));
}
private function ioerror(evt:IOErrorEvent):void{
this.dispatchEvent(new Event("IOERROR"));
}
private function onLoadComplete(evt:Event):void{
var embeddedFonts:Array = Font.enumerateFonts(false);
this.dispatchEvent(new Event("COMPLETE"));
CustomWaitAlert.hide();
}
}
}
The solution i got is to first load the font swf via url loader then we get the bytearray.after that just load tge bytearray with the loader class with loading context which is having allowcodeimport sets to true...

creating submenu's in flex context menu

Is there any workaround to create submenu in a flex context menu other than stopping right click from javascript.
Regards,
Hi Frank,
Yes, I want to create submenus in a context menu. Can you help me here.
Regards,
Hi Frank,
I need the context menu for the application not for datagrid.
In my initial question the phrase "other than stopping right click from javascript" means
"catch the right click in html, call a javascript function and over js call a as function."
The project that you have specified does the above procedure. I don't want to use this
procedure. Is there any other way for achieving submenus in a flex context menu. Could you
please tell me if so..
Regards,
Arvind
Yes, there is.
I don't know, what you exactly mean with this:
other than stopping right click from
javascript.
But, if you want to create a entry in submenu, do this:
//Instance of my own class
private var myContext:myContextMenu = new myContextMenu();
application.contextMenu = myContext.myContextMenu;
//Here is the Class:
package com.my.components
{
/* ////////////////////////////////////////////
///// My Context MenĂ¼ /////////////////////
///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//to use: //
// private var myContext:MyContextMenu = new MyContextMenu(); //
// init() in creationComplete //
// application.contextMenu = myContext.myContextMenu; //
////////////////////////////////////////////////////////////////////////////// */
import flash.display.Sprite;
import flash.events.ContextMenuEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuBuiltInItems;
import flash.ui.ContextMenuItem;
public class MyContextMenu extends Sprite
{
public var myContextMenu:ContextMenu;
private var menuLabel:String = String.fromCharCode(169)+" My Company GmbH";
public function MyContextMenu()
{
myContextMenu = new ContextMenu;
removeDefaultItems();
addCustomItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
super();
}
private function removeDefaultItems():void
{
myContextMenu.hideBuiltInItems();
var defaultItems:ContextMenuBuiltInItems = myContextMenu.builtInItems;
defaultItems.print = true;
}
private function addCustomItems():void
{
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT,menuItemSelectHandler);
}
private function menuSelectHandler(event:ContextMenuEvent):void
{
}
private function menuItemSelectHandler(event:ContextMenuEvent):void
{
navigateToURL(new URLRequest('http://www.my-company.de'));
}
private function createLabel():TextField
{
var txtField:TextField = new TextField();
//txtField.text = textLabel;
txtField.text = "RightClickHere";
return txtField;
}
}
}
Have fun
EDIT:
There is an interesting project here. They catch the right click in html, call a javascript function and over js call a as function.
Unfortunately, the limitation of FP or NativeMenu APi allowed just on level contextmenu. Read here
Frank

Problem in Flash TextField

I want to design a calculator by Flash cs5 , I use appendText Method to write the data in the textfield by the Keyboard . My problem is when I start the application I have to Click on the TextField first then type the numbers . How i can solve it .
Cheers,
Maged
What type of TextField are you using?!
Provided that you have created a dynamic TextField with the instance name of textfield, the following should work.
textfield.restrict = "0-9";
textfield.text = "";
function onKeyBoardEvent( event:KeyboardEvent ):void
{
var str:String = String.fromCharCode(event.charCode );
textfield.appendText( str);
}
you can set focus to the text field as soon as it's added to stage.
frame script:
stage.focus = textFieldInstance;
package:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class DocumentClass extends Sprite
{
public function DocumentClass()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.focus = textFieldInstance;
}
}
}

AS3: AddChild issue -- "TypeError: Error #2007: Parameter child must be non-null."

My following code gave me
TypeError: Error #2007: Parameter child must be non-null runtime error.
not sure why...I would appreciate any help...
mySb = new ScrollBar();
mySb.x = cont.x; //+ cont.width;
mySb.y = cont.y;
mySb.height = contMask.height;
mySb.enabled = true;
addChild(mySb);
Updated
package com.search.view
{
import com.search.events.YouTubeSearchEvent;
import fl.controls.ScrollBar;
import fl.controls.Slider;
import fl.controls.UIScrollBar;
import fl.events.ScrollEvent;
import fl.events.SliderEvent;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.net.URLLoader;
public class SearchResultContainer extends Sprite
{
private var cont:videoCont;
private var contMask:Sprite;
private var mySb:ScrollBar;
public function SearchResultContainer()
{
super();
}
public function get selectedVideoID():String{
return newVideoID;
}
public function createContainer(_x:Number,_y:Number, videoResult:Array):void{
cont=new videoCont();
cont.x=_x;
cont.y=_y;
addChild(cont);
contMask = new Sprite();
contMask.x = cont.x;
contMask.y = cont.y;
createMask(contMask,0x000000,452,88);
addChild(contMask);
cont.mask = contMask;
mySb = new ScrollBar();
mySb.x = cont.x; //+ cont.width;
mySb.y = cont.y;
mySb.height = contMask.height;
mySb.enabled = true;
addChild(mySb); //problem code here...
}
private function createMask(inSrc:*,inColor:Number=0x999999,inW:Number=80,inH:Number=50):void{
var rect:Shape=new Shape();
rect.graphics.clear();
rect.graphics.beginFill(inColor);
rect.graphics.drawRect(0,0,inW,inH);
rect.graphics.endFill();
inSrc.addChild(rect);
}
}
}
I am in Flex environment....
Try to add a breakpoint before the problem occurs and check the mySb value , it looks like it's probably null , if it's not you'll have to look for null values either in the DisplayObjects you're using or the properties you're assigning them... if it is null , maybe you need to set more properties to your ScrollBar instance before adding it to the display list...
In my case I solved it adding the component to the movie library, but working in Flash CS5.5 environment.

( Flex ) How can we get an imagesnapshot of the entire component without scrollbars?

I can take snapshot of a component. But the problem is the component is lil bigger with scroll bars. The saved image has scrollbars (only the visible area is getting saved). What i need is I want the entire component to be saved as an image.
This exact functionality is available while we print the component using FlexPrintJob, where by setting the FlexPrintJobScaleType.NONE.
But here in my case i want it to be saved using ImageSnapShot ( not thru FlexPrintJob ).
Thanks Advance,
Sriss
I thought I knew how to do this, but there seem to be lots of awkward issues. I got it working but it's not nice. :-( Maybe you can improve on it.
Here's the code for an example application. And below is the code for the MyCanvas class. When you click the button an image of the Canvas container but without scrollbars should be drawn.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:my="*">
<mx:Script><![CDATA[
import flash.display.BitmapData;
import flash.events.Event;
import mx.containers.Canvas;
import mx.graphics.ImageSnapshot;
import flash.geom.Matrix;
import mx.core.ScrollPolicy;
public function onclick():void
{
var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(canvas);
canvas.addEventListener("BitMapReady", onBitMapReady);
canvas.horizontalScrollPolicy = ScrollPolicy.OFF;
canvas.CreateBitMapData();
}
private function onBitMapReady(e:Event):void
{
DrawBitmapDataAt(canvas.bitMapData, 100, 100);
canvas.removeEventListener("BitMapReady", onBitMapReady);
canvas.horizontalScrollPolicy = ScrollPolicy.AUTO;
}
private function DrawBitmapDataAt(bitmapData:BitmapData,x:int,y:int):void
{
var matrix:Matrix = new Matrix();
matrix.tx = x;
matrix.ty = y;
box.graphics.lineStyle(0,0,0);
box.graphics.beginBitmapFill(bitmapData, matrix, false);
box.graphics.drawRect(x,y,bitmapData.width,bitmapData.height);
}
]]></mx:Script>
<mx:Box id="box">
<my:MyCanvas width="50" height="50" backgroundColor="white" id="canvas">
<mx:Button label="Hello" click="onclick()" />
</my:MyCanvas>
</mx:Box>
</mx:Application>
MyCanvas class:
package
{
import flash.events.Event;
import flash.events.TimerEvent;
import mx.containers.Canvas;
import flash.display.BitmapData;
import mx.core.ScrollPolicy;
import mx.graphics.ImageSnapshot;
import flash.utils.Timer;
public class MyCanvas extends Canvas
{
public var bitMapData:BitmapData;
private var creatingBitMap:Boolean = false;
private var timer:Timer;
public function CreateBitMapData():void
{
this.horizontalScrollPolicy = ScrollPolicy.OFF;
creatingBitMap = true;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if (creatingBitMap == true && this.horizontalScrollBar == null)
{
bitMapData = ImageSnapshot.captureBitmapData(this);
this.dispatchEvent(new Event("BitMapReady"));
creatingBitMap = false;
timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, onTimer);
this.width += 1;
timer.start();
}
}
private function onTimer(e:TimerEvent):void
{
this.width -= 1;
trace("timer");
timer.removeEventListener(TimerEvent.TIMER, onTimer);
timer.stop();
}
}
}

Resources