Click here to Skip to main content
15,885,862 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.2K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace CBR.Core.Helpers.Localization
{
    /// <summary>
    /// Base class for all resource providers
    /// </summary>
    internal class ProviderBase : IResourceProvider
    {
        #region ----------------OVERRIDEABLES----------------

        /// <summary>
        /// This remove a resource from memory, more for developper when update or change the module
        /// or the default value to re create the resource (files mode)
        /// </summary>
        /// <param name="code2Letter"></param>
        /// <param name="modul"></param>
        /// <param name="resource"></param>
        public virtual void DeleteResource(string code2Letter, string modul, object resource)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Return all resources for a given module, not implemented by all providers
        /// </summary>
        /// <param name="code2Letter"></param>
        /// <param name="modul"></param>
        /// <returns></returns>
        public virtual List<LocalizationItem> GetModuleResource(string code2Letter, string modul)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Return all discovered moduls, not implemented by all providers
        /// </summary>
        /// <param name="code2Letter"></param>
        /// <returns></returns>
        public virtual List<string> GetAvailableModules(string code2Letter)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Return discovered culture, not implemented by all providers
        /// </summary>
        /// <returns></returns>
        public virtual List<CultureItem> GetAvailableCultures()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Save the resource, not implemented by all providers
        /// </summary>
        public virtual void SaveDefaultResources()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Create new culture resources, not implemented by all providers
        /// </summary>
        /// <param name="info"></param>
        /// <param name="iconFile"></param>
        public virtual CultureItem CreateCulture(CultureInfo info, string iconFile)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Return the extension value, implemented by all providers
        /// </summary>
        /// <param name="ext"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public virtual object GetObject(LocalizationExtension ext, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ----------------INTERNALS----------------

        [DllImport("gdi32.dll")]
        private static extern bool DeleteObject(IntPtr hObject);

        /// <summary>
        /// Convert a resource object to the type required by the WPF element
        /// </summary>
        /// <param name="value">The resource value to convert</param>
        /// <returns>The WPF element value</returns>
        public object ConvertValue(LocalizationExtension ext, object value)
        {
            object result = null;
            BitmapSource bitmapSource = null;

            // convert icons and bitmaps to BitmapSource objects that WPF uses
            if (value is Icon)
            {
                Icon icon = value as Icon;

                // For icons we must create a new BitmapFrame from the icon data stream
                // The approach we use for bitmaps (below) doesn't work when setting the
                // Icon property of a window (although it will work for other Icons)
                //
                using (MemoryStream iconStream = new MemoryStream())
                {
                    icon.Save(iconStream);
                    iconStream.Seek(0, SeekOrigin.Begin);
                    bitmapSource = BitmapFrame.Create(iconStream);
                }
            }
            else if (value is Bitmap)
            {
                Bitmap bitmap = value as Bitmap;
                IntPtr bitmapHandle = bitmap.GetHbitmap();
                bitmapSource = Imaging.CreateBitmapSourceFromHBitmap( bitmapHandle, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
                bitmapSource.Freeze();
                DeleteObject(bitmapHandle);
            }

            if (bitmapSource != null)
            {
                // if the target property is expecting the Icon to be content then we
                // create an ImageControl and set its Source property to image
                //
                if (ext.TargetPropertyType == typeof(object))
                {
                    System.Windows.Controls.Image imageControl = new System.Windows.Controls.Image();
                    imageControl.Source = bitmapSource;
                    imageControl.Width = bitmapSource.Width;
                    imageControl.Height = bitmapSource.Height;
                    result = imageControl;
                }
                else
                {
                    result = bitmapSource;
                }
            }
            else
            {
                result = value;

                // allow for resources to either contain simple strings or typed data
                //
                Type targetType = ext.TargetPropertyType;
                if (value is String && targetType != typeof(String) && targetType != typeof(object))
                {
                    TypeConverter tc = TypeDescriptor.GetConverter(targetType);
                    result = tc.ConvertFromInvariantString(value as string);
                }
            }

            return result;
        }

        /// <summary>
        ///  Return the default value for the property
        /// </summary>
        /// <param name="ext"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public virtual object GetDefaultValue(LocalizationExtension ext, CultureInfo culture)
        {
            object result = ext.DefaultValue;
            Type targetType = ext.TargetPropertyType;

            if (ext.DefaultValue == null)
            {
                if (targetType == typeof(String) || targetType == typeof(object))
                {
                    result = "No default on #" + ext.Key;
                }
            }
            else if (targetType != null)
            {
                // convert the default value if necessary to the required type
                if (targetType != typeof(String) && targetType != typeof(object))
                {
                    try
                    {
                        TypeConverter tc = TypeDescriptor.GetConverter(targetType);
                        result = tc.ConvertFromInvariantString(ext.DefaultValue);
                    }
                    catch
                    {
                    }
                }
                else
                    result = "#" + result;
            }

            return result;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions