Esempio n. 1
0
        // run when xna is initalized.
        public void InitWithGraphicsDevice(GraphicsDevice gd)
        {
            mGL.Init(gd);

            // Send initialized event.
            MoSync.Memory eventData = new MoSync.Memory(8);
            eventData.WriteInt32(MoSync.Struct.MAWidgetEventData.eventType, MoSync.Constants.MAW_EVENT_GL_VIEW_READY);
            eventData.WriteInt32(MoSync.Struct.MAWidgetEventData.widgetHandle, 0);
            mRuntime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
        }
Esempio n. 2
0
        private void SendSensorEventVector(Runtime runtime, int type, Vector3 data)
        {
            Memory eventData = new Memory(4 * 4);

            eventData.WriteInt32(MoSync.Struct.MASensor.type, type);
            eventData.WriteInt32(MoSync.Struct.MASensor.values + 0, MoSync.Util.ConvertToInt(data.X));
            eventData.WriteInt32(MoSync.Struct.MASensor.values + 4, MoSync.Util.ConvertToInt(data.Y));
            eventData.WriteInt32(MoSync.Struct.MASensor.values + 8, MoSync.Util.ConvertToInt(data.Z));
            runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_SENSOR, eventData);
        }
Esempio n. 3
0
        public void Init(Ioctls ioctls, Core core, Runtime runtime)
        {
            ioctls.maSensorStart = delegate(int _sensor, int _interval)
            {
                _interval = GetSensorIntervalDefaults(_interval);

                TimeSpan time = TimeSpan.FromMilliseconds((double)_interval);

                if (_sensor == MoSync.Constants.SENSOR_TYPE_ACCELEROMETER &&
                    Accelerometer.IsSupported)
                {
                    if (mAccelerometer != null)
                    {
                        return(MoSync.Constants.SENSOR_ERROR_ALREADY_ENABLED);
                    }

                    mAccelerometer = new Accelerometer();
                    mAccelerometer.TimeBetweenUpdates   = time;
                    mAccelerometer.CurrentValueChanged +=
                        delegate(object sender, SensorReadingEventArgs <AccelerometerReading> args)
                    {
                        Vector3 acc = args.SensorReading.Acceleration;
                        SendSensorEventVector(runtime, MoSync.Constants.SENSOR_TYPE_ACCELEROMETER, acc);
                    };

                    mAccelerometer.Start();
                }
                else if (_sensor == MoSync.Constants.SENSOR_TYPE_GYROSCOPE &&
                         Gyroscope.IsSupported)
                {
                    if (mGyroscope != null)
                    {
                        return(MoSync.Constants.SENSOR_ERROR_ALREADY_ENABLED);
                    }

                    mGyroscope = new Gyroscope();
                    mGyroscope.TimeBetweenUpdates   = time;
                    mGyroscope.CurrentValueChanged +=
                        delegate(object sender, SensorReadingEventArgs <GyroscopeReading> args)
                    {
                        Vector3 rot = args.SensorReading.RotationRate;
                        SendSensorEventVector(runtime, MoSync.Constants.SENSOR_TYPE_GYROSCOPE, rot);
                    };

                    mGyroscope.Start();
                }
                else if ((_sensor == MoSync.Constants.SENSOR_TYPE_MAGNETIC_FIELD || _sensor == MoSync.Constants.SENSOR_TYPE_COMPASS) &&
                         Compass.IsSupported)
                {
                    if (_sensor == MoSync.Constants.SENSOR_TYPE_MAGNETIC_FIELD &&
                        mMagneticFieldEnabled == true)
                    {
                        return(MoSync.Constants.SENSOR_ERROR_ALREADY_ENABLED);
                    }

                    if (_sensor == MoSync.Constants.SENSOR_TYPE_COMPASS &&
                        mCompassEnabled == true)
                    {
                        return(MoSync.Constants.SENSOR_ERROR_ALREADY_ENABLED);
                    }

                    if (mCompass == null)
                    {
                        mCompass = new Compass();
                        mCompass.TimeBetweenUpdates = time;
                    }
                    else
                    {
                        if (time < mCompass.TimeBetweenUpdates)
                        {
                            mCompass.TimeBetweenUpdates = time;
                        }
                    }

                    if (mCompassEnabled == false && mMagneticFieldEnabled == false)
                    {
                        mCompass.CurrentValueChanged +=
                            delegate(object sender, SensorReadingEventArgs <CompassReading> args)
                        {
                            if (mMagneticFieldEnabled)
                            {
                                Vector3 rot = args.SensorReading.MagnetometerReading;
                                SendSensorEventVector(runtime, MoSync.Constants.SENSOR_TYPE_MAGNETIC_FIELD, rot);
                            }

                            if (mCompassEnabled)
                            {
                                Vector3 heading = new Vector3();
                                heading.X = (float)args.SensorReading.MagneticHeading;
                                SendSensorEventVector(runtime, MoSync.Constants.SENSOR_TYPE_COMPASS, heading);
                            }
                        };

                        mCompass.Start();
                    }

                    if (_sensor == MoSync.Constants.SENSOR_TYPE_MAGNETIC_FIELD)
                    {
                        mMagneticFieldEnabled = true;
                    }
                    else if (_sensor == MoSync.Constants.SENSOR_TYPE_COMPASS)
                    {
                        mCompassEnabled = true;
                    }
                }

#if false
                else if (_sensor == MoSync.Constants.SENSOR_TYPE_ORIENTATION &&
                         Motion.IsSupported)
                {
                    mMotion = new Motion();
                    mMotion.TimeBetweenUpdates   = new TimeSpan(intervalIn100Nanoseconds);
                    mMotion.CurrentValueChanged +=
                        delegate(object sender, SensorReadingEventArgs <MotionReading> args)
                    {
                    };
                }
#endif
                else
                {
                    return(MoSync.Constants.SENSOR_ERROR_NOT_AVAILABLE);
                }

                return(MoSync.Constants.SENSOR_ERROR_NONE);
            };

            ioctls.maSensorStop = delegate(int _sensor)
            {
                switch (_sensor)
                {
                case MoSync.Constants.SENSOR_TYPE_ACCELEROMETER:
                    if (mAccelerometer != null)
                    {
                        mAccelerometer.Stop();
                        mAccelerometer = null;
                    }
                    else
                    {
                        return(MoSync.Constants.SENSOR_ERROR_NOT_ENABLED);
                    }
                    break;

                case MoSync.Constants.SENSOR_TYPE_GYROSCOPE:
                    if (mGyroscope != null)
                    {
                        mGyroscope.Stop();
                        mGyroscope = null;
                    }
                    else
                    {
                        return(MoSync.Constants.SENSOR_ERROR_NOT_ENABLED);
                    }
                    break;

                case MoSync.Constants.SENSOR_TYPE_MAGNETIC_FIELD:
                    if (!mMagneticFieldEnabled)
                    {
                        return(MoSync.Constants.SENSOR_ERROR_NOT_ENABLED);
                    }

                    if (mCompass != null && !mCompassEnabled)
                    {
                        mCompass.Stop();
                        mCompass = null;
                    }

                    mMagneticFieldEnabled = false;
                    break;

                case MoSync.Constants.SENSOR_TYPE_COMPASS:
                    if (!mCompassEnabled)
                    {
                        return(MoSync.Constants.SENSOR_ERROR_NOT_ENABLED);
                    }

                    if (mCompass != null && !mMagneticFieldEnabled)
                    {
                        mCompass.Stop();
                        mCompass = null;
                    }
                    mCompassEnabled = false;
                    break;

                case MoSync.Constants.SENSOR_TYPE_ORIENTATION:
                    if (mMotion != null)
                    {
                        mMotion.Stop();
                        mMotion = null;
                    }
                    else
                    {
                        return(MoSync.Constants.SENSOR_ERROR_NOT_ENABLED);
                    }
                    break;
                }
                return(MoSync.Constants.SENSOR_ERROR_NONE);
            };

            ioctls.maLocationStart = delegate()
            {
                if (mGeoWatcher == null)
                {
                    mGeoWatcher = new GeoCoordinateWatcher();
                    //mGeoWatcher.MovementThreshold = 20;

                    mGeoWatcher.StatusChanged += delegate(object sender,
                                                          GeoPositionStatusChangedEventArgs args)
                    {
                        int maState;
                        switch (args.Status)
                        {
                        case GeoPositionStatus.Disabled:
                            maState = MoSync.Constants.MA_LPS_OUT_OF_SERVICE;
                            break;

                        case GeoPositionStatus.NoData:
                        case GeoPositionStatus.Initializing:
                            maState = MoSync.Constants.MA_LPS_TEMPORARILY_UNAVAILABLE;
                            break;

                        case GeoPositionStatus.Ready:
                            maState = MoSync.Constants.MA_LPS_AVAILABLE;
                            break;

                        default:
                            throw new Exception("invalid GeoPositionStatus");
                        }
                        Memory evt = new Memory(2 * 4);
                        evt.WriteInt32(MoSync.Struct.MAEvent.type, MoSync.Constants.EVENT_TYPE_LOCATION_PROVIDER);
                        evt.WriteInt32(MoSync.Struct.MAEvent.state, maState);
                        runtime.PostEvent(new Event(evt));
                    };

                    mGeoWatcher.PositionChanged += delegate(object sender,
                                                            GeoPositionChangedEventArgs <GeoCoordinate> args)
                    {
                        int maValidity = args.Position.Location.IsUnknown ?
                                         MoSync.Constants.MA_LOC_INVALID : MoSync.Constants.MA_LOC_QUALIFIED;
                        Memory        evt = new Memory(4 + 4 * 8 + 4);
                        GeoCoordinate l   = args.Position.Location;
                        evt.WriteInt32(MoSync.Struct.MALocation.state, maValidity);
                        evt.WriteDouble(MoSync.Struct.MALocation.lat, l.Latitude);
                        evt.WriteDouble(MoSync.Struct.MALocation.lon, l.Longitude);
                        evt.WriteDouble(MoSync.Struct.MALocation.horzAcc, l.HorizontalAccuracy);
                        evt.WriteDouble(MoSync.Struct.MALocation.vertAcc, l.VerticalAccuracy);
                        evt.WriteFloat(MoSync.Struct.MALocation.alt, (float)l.Altitude);
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_LOCATION, evt);
                    };

                    mGeoWatcher.Start();
                }

                return(0);
            };

            ioctls.maLocationStop = delegate()
            {
                if (mGeoWatcher != null)
                {
                    mGeoWatcher.Stop();
                    mGeoWatcher = null;
                }

                return(0);
            };
        }
Esempio n. 4
0
        public void Init(Ioctls ioctls, Core core, Runtime runtime)
        {
            mNativeUI = new NativeUI.AsyncNativeUIWindowsPhone(runtime);
            //mWidgets.Add(null); // why?

            // initialize the widget thread dictionary
            mWidgetThreadDictionary = new Dictionary <int, Thread>();

            mWidgetTypeDictionary = new Dictionary <int, Type>();

            /**
             * This will add a OrientationChanged event handler to the Application.Current.RootVisual, this is application wide.
             */
            (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).OrientationChanged += delegate(object from, Microsoft.Phone.Controls.OrientationChangedEventArgs args)
            {
                PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage);

                int mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_PORTRAIT_UP;
                switch (currentPage.Orientation)
                {
                case PageOrientation.Landscape:
                    mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_LANDSCAPE;
                    break;

                case PageOrientation.LandscapeLeft:
                    mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_LANDSCAPE_LEFT;
                    break;

                case PageOrientation.LandscapeRight:
                    mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_LANDSCAPE_RIGHT;
                    break;

                case PageOrientation.Portrait:
                    mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_PORTRAIT_UP;
                    break;

                case PageOrientation.PortraitDown:
                    mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_PORTRAIT_UPSIDE_DOWN;
                    break;

                case PageOrientation.PortraitUp:
                    mosyncScreenOrientation = MoSync.Constants.MA_SCREEN_ORIENTATION_PORTRAIT_UP;
                    break;
                }

                // Post event handled Moblet.
                Memory    eventData               = new Memory(8);
                const int MAEventData_eventType   = 0;
                const int MAEventData_orientation = 4;
                eventData.WriteInt32(MAEventData_eventType, MoSync.Constants.EVENT_TYPE_ORIENTATION_DID_CHANGE);
                eventData.WriteInt32(MAEventData_orientation, mosyncScreenOrientation);

                runtime.PostEvent(new Event(eventData));
            };

            ioctls.maWidgetCreate = delegate(int _widgetType)
            {
                String widgetTypeName = core.GetDataMemory().ReadStringAtAddress(_widgetType);

                Type widgetType = mNativeUI.VerifyWidget(widgetTypeName);
                if (widgetType == null)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_TYPE_NAME);
                }
                IWidget widget = new WidgetBaseMock();
                widget.SetRuntime(runtime);

                int widgetHandle = FindSpaceForWidget();
                if (widgetHandle == -1)
                {
                    mWidgets.Add(widget);
                    widgetHandle = mWidgets.Count - 1;
                }
                else
                {
                    mWidgets[widgetHandle] = widget;
                }
                widget.SetHandle(widgetHandle);
                StartWidgetCreationThread(widgetHandle, widgetType);

                return(widgetHandle);
            };

            ioctls.maWidgetDestroy = delegate(int _widget)
            {
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget widget = mWidgets[_widget];
                if (widget != null)
                {
                    widget.RemoveFromParent();
                    mWidgets[_widget] = null;

                    mWidgetTypeDictionary.Remove(_widget);
                    Thread widgetCreationThread = null;
                    mWidgetThreadDictionary.TryGetValue(_widget, out widgetCreationThread);
                    if (widgetCreationThread != null)
                    {
                        if (widgetCreationThread.IsAlive)
                        {
                            widgetCreationThread.Abort();
                        }
                        mWidgetThreadDictionary.Remove(_widget);
                    }
                }
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetAddChild = delegate(int _parent, int _child)
            {
                if (_parent < 0 || _parent >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                if (_child < 0 || _child >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IWidget parent = mWidgets[_parent];
                IWidget child  = mWidgets[_child];
                mNativeUI.AddChild(parent, child);

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetRemoveChild = delegate(int _child)
            {
                if (_child < 0 || _child >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IWidget child = mWidgets[_child];
                // only the child is needed - it has a reference to its parent
                mNativeUI.RemoveChild(child);

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetInsertChild = delegate(int _parent, int _child, int index)
            {
                if (_parent < 0 || _parent >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                if (_child < 0 || _child >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IWidget parent = mWidgets[_parent];
                IWidget child  = mWidgets[_child];
                mNativeUI.InsertChild(parent, child, index);

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetStackScreenPush = delegate(int _stackScreen, int _newScreen)
            {
                IScreen stackScreen = (IScreen)mWidgets[_stackScreen];
                IScreen newScreen   = (IScreen)mWidgets[_newScreen];
                (stackScreen as MoSync.NativeUI.StackScreen).Push(newScreen);
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetStackScreenPop = delegate(int _stackScreen)
            {
                IScreen stackScreen = (IScreen)mWidgets[_stackScreen];
                (stackScreen as MoSync.NativeUI.StackScreen).Pop();
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetSetProperty = delegate(int _widget, int _property, int _value)
            {
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                String  property = core.GetDataMemory().ReadStringAtAddress(_property);
                String  value    = core.GetDataMemory().ReadStringAtAddress(_value);
                IWidget widget   = mWidgets[_widget];
                try
                {
                    mNativeUI.SetProperty(widget, property, value);
                }
                catch (InvalidPropertyNameException)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_PROPERTY_NAME);
                }
                catch (InvalidPropertyValueException)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_PROPERTY_VALUE);
                }

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetGetProperty = delegate(int _widget, int _property, int _value, int _bufSize)
            {
                String property = core.GetDataMemory().ReadStringAtAddress(_property);
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget widget = mWidgets[_widget];
                try
                {
                    // String value = widget.GetProperty(property);
                    String value = mNativeUI.GetProperty(widget, property);
                    core.GetDataMemory().WriteStringAtAddress(_value, value, _bufSize);
                }
                catch (InvalidPropertyNameException e)
                {
                    MoSync.Util.Log(e);
                    return(MoSync.Constants.MAW_RES_INVALID_PROPERTY_NAME);
                }

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetScreenShow = delegate(int _screenHandle)
            {
                if (_screenHandle < 0 || _screenHandle >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IScreen screen = null;

                if (mWidgets[_screenHandle] is IScreen)
                {
                    screen = (IScreen)mWidgets[_screenHandle];
                }
                else
                {
                    return(MoSync.Constants.MAW_RES_INVALID_SCREEN);
                }

                mCurrentScreen = screen;

                screen.Show();
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetScreenShowWithTransition = delegate(int _screenHandle, int _screenTransitionType, int _screenTransitionDuration)
            {
                // Windows Phone Toolkit screen transitions do not have an time argument so _screenTransitionDuration
                // will be ignored on Windows platform.
                if (_screenHandle < 0 || _screenHandle >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IScreen screen = null;

                if (mWidgets[_screenHandle] is IScreen)
                {
                    screen = (IScreen)mWidgets[_screenHandle];
                }
                else
                {
                    return(MoSync.Constants.MAW_RES_INVALID_SCREEN);
                }

                mCurrentScreen = screen;

                // If transition type is not available on this platform do show without transitions but return error code.
                if (!NativeUI.MoSyncScreenTransitions.isTransitionAvailable(_screenTransitionType))
                {
                    screen.ShowWithTransition(MoSync.Constants.MAW_TRANSITION_TYPE_NONE);
                    return(MoSync.Constants.MAW_RES_INVALID_SCREEN_TRANSITION_TYPE);
                }

                screen.ShowWithTransition(_screenTransitionType);
                return(MoSync.Constants.MAW_RES_OK);
            };

            /*
             * Implementation for maWidgetScreenAddOptionsMenuItem
             *
             * @param _widget the widget handle
             * @param _title the option menu item title
             * @param _iconPath the option menu item path
             *        Note: if the _iconPredefined param is 1 then the _iconPath
             *              will store a code representing the name of the icon file,
             *              without extension. Otherwise it should contain the name of the
             *              file. (e.g. "applicationBarIcon1.png")
             * @param _iconPredefined if the value is 1 it means that we expect a predefined icon
             *        otherwise it will create the path using the _iconPath as it was previously
             *        explained
             */
            ioctls.maWidgetScreenAddOptionsMenuItem = delegate(int _widget, int _title, int _iconPath, int _iconPredefined)
            {
                //This represents the hardcoded folder name for the application bar icons
                String applicationBarIconsFolder = "/AppBar.Icons/";

                //if _widget < 0 => no screen parent
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IScreen screen = (IScreen)mWidgets[_widget];

                //Read the icon path
                string iconPath = core.GetDataMemory().ReadStringAtAddress(_iconPath);

                //If the iconPath is not empty and we don't have a predefined icon
                //then we have an ApplicationBarButton object with a given icon and text.
                if (!iconPath.Equals("") && 0 == _iconPredefined && screen.GetApplicationBar().Buttons.Count < 5)
                {
                    //Read the text
                    string buttonText = core.GetDataMemory().ReadStringAtAddress(_title);

                    //Create the native object.
                    Microsoft.Phone.Shell.ApplicationBarIconButton btn = new Microsoft.Phone.Shell.ApplicationBarIconButton();

                    //Create the icon path.
                    btn.IconUri = new Uri(applicationBarIconsFolder + iconPath, UriKind.RelativeOrAbsolute);
                    btn.Text    = buttonText;

                    //Associate an index to the native object.
                    int btnIndex = screen.AddApplicationBarItemIndex(btn);

                    btn.Click += new EventHandler(
                        delegate(object from, EventArgs target)
                    {
                        Memory eventData = new Memory(12);
                        const int MAWidgetEventData_eventType    = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_itemIndex    = 8;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_OPTIONS_MENU_ITEM_SELECTED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, _widget);
                        eventData.WriteInt32(MAWidgetEventData_itemIndex, btnIndex);
                        //Posting a CustomEvent
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    });

                    screen.GetApplicationBar().Buttons.Add(btn);
                    screen.EnableApplicationBar();
                    return(btnIndex);
                }
                //If the iconPath is not empty and we have a predefined icon
                //then we have an ApplicationBarButton object with a predefined icon and text.
                else if (!iconPath.Equals("") && _iconPredefined > 0 && screen.GetApplicationBar().Buttons.Count < 5)
                {
                    //Read the text.
                    string buttonText = core.GetDataMemory().ReadStringAtAddress(_title);

                    //Create the native object.
                    Microsoft.Phone.Shell.ApplicationBarIconButton btn = new Microsoft.Phone.Shell.ApplicationBarIconButton();

                    //Create the icon path.
                    btn.IconUri = new Uri(applicationBarIconsFolder + iconPath + ".png", UriKind.RelativeOrAbsolute);
                    btn.Text    = buttonText;

                    //Associate an index to the native object.
                    int btnIndex = screen.AddApplicationBarItemIndex(btn);

                    btn.Click += new EventHandler(
                        delegate(object from, EventArgs target)
                    {
                        Memory eventData = new Memory(12);
                        const int MAWidgetEventData_eventType    = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_itemIndex    = 8;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_OPTIONS_MENU_ITEM_SELECTED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, _widget);
                        eventData.WriteInt32(MAWidgetEventData_itemIndex, btnIndex);
                        //Posting a CustomEvent
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    });

                    screen.GetApplicationBar().Buttons.Add(btn);
                    screen.EnableApplicationBar();

                    //Return the index associated to the item.
                    return(btnIndex);
                }
                //If the iconPath is empty then we have an ApplicationBarMenuItem.
                else
                {
                    //Read the text.
                    string menuItemText = core.GetDataMemory().ReadStringAtAddress(_title);

                    //Create the native object.
                    Microsoft.Phone.Shell.ApplicationBarMenuItem menuItem = new Microsoft.Phone.Shell.ApplicationBarMenuItem();
                    menuItem.Text = menuItemText;

                    //Associate an index to the native object.
                    int menuIndex = screen.AddApplicationBarItemIndex(menuItem);

                    menuItem.Click += new EventHandler(
                        delegate(object from, EventArgs target)
                    {
                        Memory eventData = new Memory(12);
                        const int MAWidgetEventData_eventType    = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_itemIndex    = 8;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_OPTIONS_MENU_ITEM_SELECTED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, _widget);
                        eventData.WriteInt32(MAWidgetEventData_itemIndex, menuIndex);
                        //Posting a CustomEvent
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    });

                    screen.GetApplicationBar().MenuItems.Add(menuItem);
                    screen.EnableApplicationBar();

                    //Return the index associated to the item.
                    return(menuIndex);
                }
            };
        }
Esempio n. 5
0
        public void Init(Ioctls ioctls, Core core, Runtime runtime)
        {
            mNativeUI = new NativeUI.NativeUIWindowsPhone();
            //mWidgets.Add(null); // why?

            ioctls.maWidgetCreate = delegate(int _widgetType)
            {
                String  widgetType = core.GetDataMemory().ReadStringAtAddress(_widgetType);
                IWidget widget     = mNativeUI.CreateWidget(widgetType);
                if (widget == null)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_TYPE_NAME);
                }

                widget.SetRuntime(runtime);

                for (int i = 0; i < mWidgets.Count; i++)
                {
                    if (mWidgets[i] == null)
                    {
                        widget.SetHandle(i);
                        mWidgets[i] = widget;
                        return(i);
                    }
                }

                mWidgets.Add(widget);
                widget.SetHandle(mWidgets.Count - 1);
                return(mWidgets.Count - 1);
            };

            ioctls.maWidgetDestroy = delegate(int _widget)
            {
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget widget = mWidgets[_widget];
                if (widget != null)
                {
                    widget.RemoveFromParent();
                    mWidgets[_widget] = null;
                }
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetAddChild = delegate(int _parent, int _child)
            {
                if (_parent < 0 || _parent >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget parent = mWidgets[_parent];
                if (_child < 0 || _child >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget child = mWidgets[_child];
                child.SetParent(parent);
                parent.AddChild(child);
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetRemoveChild = delegate(int _child)
            {
                if (_child < 0 || _child >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget child = mWidgets[_child];
                child.RemoveFromParent();
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetInsertChild = delegate(int _parent, int _child, int index)
            {
                if (_parent < 0 || _parent >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget parent = mWidgets[_parent];
                if (_child < 0 || _child >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget child = mWidgets[_child];
                child.SetParent(parent);
                parent.InsertChild(child, index);
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetStackScreenPush = delegate(int _stackScreen, int _newScreen)
            {
                IScreen stackScreen = (IScreen)mWidgets[_stackScreen];
                IScreen newScreen   = (IScreen)mWidgets[_newScreen];
                (stackScreen as MoSync.NativeUI.StackScreen).Push(newScreen);
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetStackScreenPop = delegate(int _stackScreen)
            {
                IScreen stackScreen = (IScreen)mWidgets[_stackScreen];
                (stackScreen as MoSync.NativeUI.StackScreen).Pop();
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetSetProperty = delegate(int _widget, int _property, int _value)
            {
                String property = core.GetDataMemory().ReadStringAtAddress(_property);
                String value    = core.GetDataMemory().ReadStringAtAddress(_value);
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget widget = mWidgets[_widget];
                try
                {
                    widget.SetProperty(property, value);
                }
                catch (InvalidPropertyNameException)
                {
                    MoSync.Util.Log(widget.GetType().ToString() + " invalid property name: " + property);
                    return(MoSync.Constants.MAW_RES_INVALID_PROPERTY_NAME);
                }
                catch (InvalidPropertyValueException e)
                {
                    MoSync.Util.Log(e);
                    return(MoSync.Constants.MAW_RES_INVALID_PROPERTY_VALUE);
                }

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetGetProperty = delegate(int _widget, int _property, int _value, int _bufSize)
            {
                String property = core.GetDataMemory().ReadStringAtAddress(_property);
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }
                IWidget widget = mWidgets[_widget];
                try
                {
                    String value = widget.GetProperty(property);
                    core.GetDataMemory().WriteStringAtAddress(_value, value, _bufSize);
                }
                catch (InvalidPropertyNameException e)
                {
                    MoSync.Util.Log(e);
                    return(MoSync.Constants.MAW_RES_INVALID_PROPERTY_NAME);
                }

                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetScreenShow = delegate(int _screenHandle)
            {
                if (_screenHandle < 0 || _screenHandle >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IScreen screen = null;

                if (mWidgets[_screenHandle] is IScreen)
                {
                    screen = (IScreen)mWidgets[_screenHandle];
                }
                else
                {
                    return(MoSync.Constants.MAW_RES_INVALID_SCREEN);
                }

                mCurrentScreen = screen;

                screen.Show();
                return(MoSync.Constants.MAW_RES_OK);
            };

            ioctls.maWidgetScreenShowWithTransition = delegate(int _screenHandle, int _screenTransitionType, int _screenTransitionDuration)
            {
                // Windows Phone Toolkit screen transitions do not have an time argument so _screenTransitionDuration
                // will be ignored on Windows platform.
                if (_screenHandle < 0 || _screenHandle >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IScreen screen = null;

                if (mWidgets[_screenHandle] is IScreen)
                {
                    screen = (IScreen)mWidgets[_screenHandle];
                }
                else
                {
                    return(MoSync.Constants.MAW_RES_INVALID_SCREEN);
                }

                mCurrentScreen = screen;

                // If transition type is not available on this platform do show without transitions but return error code.
                if (!NativeUI.MoSyncScreenTransitions.isTransitionAvailable(_screenTransitionType))
                {
                    screen.ShowWithTransition(MoSync.Constants.MAW_TRANSITION_TYPE_NONE);
                    return(MoSync.Constants.MAW_RES_INVALID_SCREEN_TRANSITION_TYPE);
                }

                screen.ShowWithTransition(_screenTransitionType);
                return(MoSync.Constants.MAW_RES_OK);
            };

            /*
             * Implementation for maWidgetScreenAddOptionsMenuItem
             *
             * @param _widget the widget handle
             * @param _title the option menu item title
             * @param _iconPath the option menu item path
             *        Note: if the _iconPredefined param is 1 then the _iconPath
             *              will store a code representing the name of the icon file,
             *              without extension. Otherwise it should contain the name of the
             *              file. (e.g. "applicationBarIcon1.png")
             * @param _iconPredefined if the value is 1 it means that we expect a predefined icon
             *        otherwise it will create the path using the _iconPath as it was previously
             *        explained
             */
            ioctls.maWidgetScreenAddOptionsMenuItem = delegate(int _widget, int _title, int _iconPath, int _iconPredefined)
            {
                //This represents the hardcoded folder name for the application bar icons
                String applicationBarIconsFolder = "/AppBar.Icons/";

                //if _widget < 0 => no screen parent
                if (_widget < 0 || _widget >= mWidgets.Count)
                {
                    return(MoSync.Constants.MAW_RES_INVALID_HANDLE);
                }

                IScreen screen = (IScreen)mWidgets[_widget];

                //Read the icon path
                string iconPath = core.GetDataMemory().ReadStringAtAddress(_iconPath);

                //If the iconPath is not empty and we don't have a predefined icon
                //then we have an ApplicationBarButton object with a given icon and text.
                if (!iconPath.Equals("") && 0 == _iconPredefined && screen.GetApplicationBar().Buttons.Count < 5)
                {
                    //Read the text
                    string buttonText = core.GetDataMemory().ReadStringAtAddress(_title);

                    //Create the native object.
                    Microsoft.Phone.Shell.ApplicationBarIconButton btn = new Microsoft.Phone.Shell.ApplicationBarIconButton();

                    //Create the icon path.
                    btn.IconUri = new Uri(applicationBarIconsFolder + iconPath, UriKind.RelativeOrAbsolute);
                    btn.Text    = buttonText;

                    //Associate an index to the native object.
                    int btnIndex = screen.AddApplicationBarItemIndex(btn);

                    btn.Click += new EventHandler(
                        delegate(object from, EventArgs target)
                    {
                        Memory eventData = new Memory(12);
                        const int MAWidgetEventData_eventType    = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_itemIndex    = 8;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_OPTIONS_MENU_ITEM_SELECTED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, _widget);
                        eventData.WriteInt32(MAWidgetEventData_itemIndex, btnIndex);
                        //Posting a CustomEvent
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    });

                    screen.GetApplicationBar().Buttons.Add(btn);
                    screen.EnableApplicationBar();
                    return(btnIndex);
                }
                //If the iconPath is not empty and we have a predefined icon
                //then we have an ApplicationBarButton object with a predefined icon and text.
                else if (!iconPath.Equals("") && _iconPredefined > 0 && screen.GetApplicationBar().Buttons.Count < 5)
                {
                    //Read the text.
                    string buttonText = core.GetDataMemory().ReadStringAtAddress(_title);

                    //Create the native object.
                    Microsoft.Phone.Shell.ApplicationBarIconButton btn = new Microsoft.Phone.Shell.ApplicationBarIconButton();

                    //Create the icon path.
                    btn.IconUri = new Uri(applicationBarIconsFolder + iconPath + ".png", UriKind.RelativeOrAbsolute);
                    btn.Text    = buttonText;

                    //Associate an index to the native object.
                    int btnIndex = screen.AddApplicationBarItemIndex(btn);

                    btn.Click += new EventHandler(
                        delegate(object from, EventArgs target)
                    {
                        Memory eventData = new Memory(12);
                        const int MAWidgetEventData_eventType    = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_itemIndex    = 8;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_OPTIONS_MENU_ITEM_SELECTED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, _widget);
                        eventData.WriteInt32(MAWidgetEventData_itemIndex, btnIndex);
                        //Posting a CustomEvent
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    });

                    screen.GetApplicationBar().Buttons.Add(btn);
                    screen.EnableApplicationBar();

                    //Return the index associated to the item.
                    return(btnIndex);
                }
                //If the iconPath is empty then we have an ApplicationBarMenuItem.
                else
                {
                    //Read the text.
                    string menuItemText = core.GetDataMemory().ReadStringAtAddress(_title);

                    //Create the native object.
                    Microsoft.Phone.Shell.ApplicationBarMenuItem menuItem = new Microsoft.Phone.Shell.ApplicationBarMenuItem();
                    menuItem.Text = menuItemText;

                    //Associate an index to the native object.
                    int menuIndex = screen.AddApplicationBarItemIndex(menuItem);

                    menuItem.Click += new EventHandler(
                        delegate(object from, EventArgs target)
                    {
                        Memory eventData = new Memory(12);
                        const int MAWidgetEventData_eventType    = 0;
                        const int MAWidgetEventData_widgetHandle = 4;
                        const int MAWidgetEventData_itemIndex    = 8;
                        eventData.WriteInt32(MAWidgetEventData_eventType, MoSync.Constants.MAW_EVENT_OPTIONS_MENU_ITEM_SELECTED);
                        eventData.WriteInt32(MAWidgetEventData_widgetHandle, _widget);
                        eventData.WriteInt32(MAWidgetEventData_itemIndex, menuIndex);
                        //Posting a CustomEvent
                        runtime.PostCustomEvent(MoSync.Constants.EVENT_TYPE_WIDGET, eventData);
                    });

                    screen.GetApplicationBar().MenuItems.Add(menuItem);
                    screen.EnableApplicationBar();

                    //Return the index associated to the item.
                    return(menuIndex);
                }
            };
        }