Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

C.B.R.

, 22 Oct 2012
Comic and electronic publication reader with library management, extended file conversion, and devices support.
CBR.zip
Release
CBR.Core.dll
CBR.exe
Dependencies
7z.dll
Fluent.dll
itextsharp.dll
Microsoft.Expression.Drawing.dll
Microsoft.Expression.Interactions.dll
SevenZipSharp.dll
System.Windows.Interactivity.dll
cbr0_5.zip
Languages
flag_france.png
flag_great_britain.png
Dependencies
7z.dll
cbrz.ico
epub.ico
pdf.ico
rar.ico
cbr_0_3-source.zip
CBR
CBR.Core
Dependencies
7z.dll
CoreDiagram.cd
itextsharp.dll
SevenZipSharp.dll
Files
Conversion
IMG
PDF
RAR
XPS
Helpers
MVVM
Commands
Events
Messages
Models
Workspace
Properties
Services
Book
CBR
Components
Controls
Converters
Designer
Adorners
Dialogs
Helpers
Dependencies
Fluent.dll
ProgrammDiagram.cd
Properties
Settings.settings
Resources
Images
16x16
pin_no.png
pin_yes.png
sort_alpha.png
sort_category.png
view_details.png
view_thumb.png
32x32
book.png
book_add.png
book_close.png
book_convert.png
book_delete.png
book_edit.png
book_open.png
book_protect.png
book_read.png
book_save.png
book_test.png
book_type_none.png
book_type_rar.png
book_type_xps.png
book_type_zip.png
exit.png
fit_height.png
fit_width.png
forbidden.png
full_screen.png
help.png
info.png
library_close.png
library_new.png
library_open.png
library_view.png
open_folder.png
page_clear_mark.png
page_goto_mark.png
page_mark.png
page_test.png
picture_order.png
picture_time.png
picture_zone.png
push_pin.png
read.png
refresh.png
save.png
save_as.png
tools.png
book.ico
phone_horizontal.png
search.png
XAML
Controls
ViewModels
Book
Explorer
File
Views
Books
File
cbr_0_4-source.zip
BookReader
BookReader.vssscc
BookReader
BookReader.csproj.vspscc
Common
Controls
Dependencies
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
Dialogs
Properties
app.manifest
Settings.settings
Reader
Catalog
Common
Images
RAR
Resources
book.ico
Images
acroread.png
document.png
ecran.png
exit.png
filter.png
FitHeight.png
FitWidth.png
forbidden.png
GotoPage.png
info.png
open_folder.png
protect.png
refresh.png
Search.png
Style.png
TheRedPushPin.png
tools.png
Themes
Black
Blue
Silver
XAML
BuildProcessTemplates
CBR.Core.csproj.vspscc
7z.dll
CoreDiagram.cd
itextsharp.dll
SevenZipSharp.dll
DragDrop
ePUB
HTML
WMI
XPS
ePUB
Books
CBR.vssscc
CBR.csproj.vspscc
Devices.xlsx
Fluent.dll
ProgrammDiagram.cd
Settings.settings
disk.png
file.png
folder.png
pin_no.png
pin_yes.png
sort_alpha.png
sort_category.png
view_details.png
view_thumb.png
book.png
book_add.png
book_close.png
book_convert.png
book_delete.png
book_edit.png
book_open.png
book_protect.png
book_read.png
book_remove.png
book_save.png
book_test.png
book_type_epub.png
book_type_none.png
book_type_rar.png
book_type_xps.png
book_type_zip.png
device_refresh.png
device_sync.png
device_view.png
exit.png
fit_height.png
fit_width.png
forbidden.png
full_screen.png
help.png
info.png
library_close.png
library_new.png
library_open.png
library_view.png
open_folder.png
page_clear_mark.png
page_first.png
page_goto_mark.png
page_last.png
page_mark.png
page_next.png
page_previous.png
page_test.png
picture_order.png
picture_time.png
picture_zone.png
push_pin.png
read.png
refresh.png
save.png
save_as.png
tools.png
book.ico
phone_horizontal.png
search.png
Books
Devices
SysItems
Devices
cbr_0_5-source.zip
BookReader.vssscc
BookReader.csproj.vspscc
7z.dll
pdftohtml.exe
SevenZipSharp.dll
WPFToolkit.dll
app.manifest
Settings.settings
book.ico
acroread.png
document.png
ecran.png
exit.png
filter.png
FitHeight.png
FitWidth.png
forbidden.png
GotoPage.png
info.png
open_folder.png
protect.png
refresh.png
Search.png
Style.png
TheRedPushPin.png
tools.png
CBR.Core.csproj.vspscc
7z.dll
cbrz.ico
CoreDiagram.cd
epub.ico
itextsharp.dll
pdf.ico
rar.ico
SevenZipSharp.dll
Files
Localization
Core
Providers
Files
Resx
Mediator
Settings.settings
CBR.vssscc
CBR.csproj.vspscc
Devices.xlsx
Fluent.dll
ProgrammDiagram.cd
Languages
flag_france.png
flag_great_britain.png
Settings.settings
disk.png
file.png
folder.png
pin_no.png
pin_yes.png
sort_alpha.png
sort_category.png
view_details.png
view_thumb.png
book.png
book_add.png
book_close.png
book_convert.png
book_delete.png
book_edit.png
book_new.png
book_open.png
book_protect.png
book_read.png
book_remove.png
book_save.png
book_test.png
book_type_epub.png
book_type_none.png
book_type_rar.png
book_type_xps.png
book_type_zip.png
delete.png
device_refresh.png
device_sync.png
device_view.png
exit.png
fit_height.png
fit_width.png
forbidden.png
full_screen.png
help.png
info.png
is_read.png
library_close.png
library_new.png
library_open.png
library_view.png
localize.png
localize_new.png
localize_select.png
lock.png
open_folder.png
page_clear_mark.png
page_first.png
page_goto_mark.png
page_last.png
page_mark.png
page_next.png
page_previous.png
page_test.png
picture_order.png
picture_time.png
picture_zone.png
push_pin.png
read.png
refresh.png
resfresh.png
save.png
save_as.png
search.png
tools.png
book.ico
phone_horizontal.png
search.png
themes
Extended
Menus
Others
Explorer
Others
c__users_sean.office_desktop_holding_ground_cbr.zip
CBR.Core.dll
CBR.Core.pdb
CBR.exe
CBR.pdb
CBR.vshost.exe
7z.dll
cbl.ico
cbrz.ico
epub.ico
pdf.ico
rar.ico
Fluent.dll
itextsharp.dll
Languages
flag_france.png
flag_great_britain.png
Microsoft.Expression.Drawing.dll
Microsoft.Expression.Interactions.dll
SevenZipSharp.dll
System.Windows.Interactivity.dll
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;

namespace Infralution.Localization.Wpf
{
    /// <summary>
    /// Defines a type converter for enum values that converts enum values to 
    /// and from string representations using resources
    /// </summary>
    /// <remarks>
    /// This class makes localization of display values for enums in a project easy.  Simply
    /// derive a class from this class and pass the ResourceManagerin the constructor.  
    /// 
    /// <code lang="C#" escaped="true" >
    /// class LocalizedEnumConverter : ResourceEnumConverter
    /// {
    ///    public LocalizedEnumConverter(Type type)
    ///        : base(type, Properties.Resources.ResourceManager)
    ///    {
    ///    }
    /// }    
    /// </code>
    /// 
    /// <code lang="Visual Basic" escaped="true" >
    /// Public Class LocalizedEnumConverter
    /// 
    ///    Inherits ResourceEnumConverter
    ///    Public Sub New(ByVal sType as Type)
    ///        MyBase.New(sType, My.Resources.ResourceManager)
    ///    End Sub
    /// End Class    
    /// </code>
    /// 
    /// Then define the enum values in the resource editor.   The names of
    /// the resources are simply the enum value prefixed by the enum type name with an
    /// underscore separator eg MyEnum_MyValue.  You can then use the TypeConverter attribute
    /// to make the LocalizedEnumConverter the default TypeConverter for the enums in your
    /// project.
    /// </remarks>
    public class ResourceEnumConverter : EnumConverter
    {
        private class LookupTable : Dictionary<string, object> { }
        private Dictionary<CultureInfo, LookupTable> _lookupTables = new Dictionary<CultureInfo, LookupTable>();
        private System.Resources.ResourceManager _resourceManager;
        private bool _isFlagEnum = false;
        private Array _flagValues;

        /// <summary>
        /// Get the lookup table for the given culture (creating if necessary)
        /// </summary>
        /// <param name="culture"></param>
        /// <returns></returns>
        private LookupTable GetLookupTable(CultureInfo culture)
        {
            LookupTable result = null;
            if (culture == null)
                culture = CultureInfo.CurrentCulture;

            if (!_lookupTables.TryGetValue(culture, out result))
            {
                result = new LookupTable();
                foreach (object value in GetStandardValues())
                {
                    string text = GetValueText(culture, value);
                    if (text != null)
                    {
                        result.Add(text, value);
                    }
                }
                _lookupTables.Add(culture, result);
            }
            return result;
        }

        /// <summary>
        /// Return the text to display for a simple value in the given culture
        /// </summary>
        /// <param name="culture">The culture to get the text for</param>
        /// <param name="value">The enum value to get the text for</param>
        /// <returns>The localized text</returns>
        private string GetValueText(CultureInfo culture, object value)
        {
            Type type = value.GetType();
            string resourceName = string.Format("{0}_{1}", type.Name, value.ToString());
            string result = _resourceManager.GetString(resourceName, culture);
            if (result == null)
                result = resourceName;
            return result;
        }

        /// <summary>
        /// Return true if the given value is can be represented using a single bit
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool IsSingleBitValue(ulong value)
        {
            switch (value)
            {
                case 0:
                    return false;
                case 1:
                    return true;
            }
            return ((value & (value - 1)) == 0);
        }

        /// <summary>
        /// Return the text to display for a flag value in the given culture
        /// </summary>
        /// <param name="culture">The culture to get the text for</param>
        /// <param name="value">The flag enum value to get the text for</param>
        /// <returns>The localized text</returns>
        private string GetFlagValueText(CultureInfo culture, object value)
        {
            // if there is a standard value then use it
            //
            if (Enum.IsDefined(value.GetType(), value))
            {
                return GetValueText(culture, value);
            }

            // otherwise find the combination of flag bit values
            // that makes up the value
            //
            ulong lValue = Convert.ToUInt32(value);
            string result = null;
            foreach (object flagValue in _flagValues)
            {
                ulong lFlagValue = Convert.ToUInt32(flagValue);
                if (IsSingleBitValue(lFlagValue))
                {
                    if ((lFlagValue & lValue) == lFlagValue)
                    {
                        string valueText = GetValueText(culture, flagValue);
                        if (result == null)
                        {
                            result = valueText;
                        }
                        else
                        {
                            result = string.Format("{0}, {1}", result, valueText);
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// Return the Enum value for a simple (non-flagged enum)
        /// </summary>
        /// <param name="culture">The culture to convert using</param>
        /// <param name="text">The text to convert</param>
        /// <returns>The enum value</returns>
        private object GetValue(CultureInfo culture, string text)
        {
            LookupTable lookupTable = GetLookupTable(culture);
            object result = null;
            lookupTable.TryGetValue(text, out result);
            return result;
        }

        /// <summary>
        /// Return the Enum value for a flagged enum
        /// </summary>
        /// <param name="culture">The culture to convert using</param>
        /// <param name="text">The text to convert</param>
        /// <returns>The enum value</returns>
        private object GetFlagValue(CultureInfo culture, string text)
        {
            LookupTable lookupTable = GetLookupTable(culture);
            string[] textValues = text.Split(',');
            ulong result = 0;
            foreach (string textValue in textValues)
            {
                object value = null;
                string trimmedTextValue = textValue.Trim();
                if (!lookupTable.TryGetValue(trimmedTextValue, out value))
                {
                    return null;
                }
                result |= Convert.ToUInt32(value);
            }
            return Enum.ToObject(EnumType, result);
        }

        /// <summary>
        /// Create a new instance of the converter using translations from the given resource manager
        /// </summary>
        /// <param name="type"></param>
        /// <param name="resourceManager"></param>
        public ResourceEnumConverter(Type type, System.Resources.ResourceManager resourceManager)
            : base(type)
        {
            _resourceManager = resourceManager;
            object[] flagAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), true);
            _isFlagEnum = flagAttributes.Length > 0;
            if (_isFlagEnum)
            {
                _flagValues = Enum.GetValues(type);
            }
        }

        /// <summary>
        /// Convert string values to enum values
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (culture == null)
                culture = CultureInfo.CurrentCulture;
            if (value is string)
            {
                object result = (_isFlagEnum) ?
                    GetFlagValue(culture, (string)value): GetValue(culture, (string)value);
                if (result == null)
                {
                    result = base.ConvertFrom(context, culture, value);
                }
                return result;
            }
            else
            {
                return base.ConvertFrom(context, culture, value);
            }
        }

        /// <summary>
        /// Convert the enum value to a string
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value"></param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (culture == null)
                culture = CultureInfo.CurrentCulture;
            if (value != null && destinationType == typeof(string))
            {
                object result = (_isFlagEnum) ? 
                    GetFlagValueText(culture, value) : GetValueText(culture, value);
                return result;
            }
            else
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }

        /// <summary>
        /// Convert the given enum value to string using the registered type converter
        /// </summary>
        /// <param name="value">The enum value to convert to string</param>
        /// <returns>The localized string value for the enum</returns>
        static public string ConvertToString(Enum value)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(value.GetType());
            return converter.ConvertToString(value);
        }

        /// <summary>
        /// Return a list of the enum values and their associated display text for the given enum type
        /// </summary>
        /// <param name="enumType">The enum type to get the values for</param>
        /// <param name="culture">The culture to get the text for</param>
        /// <returns>
        /// A list of KeyValuePairs where the key is the enum value and the value is the text to display
        /// </returns>
        /// <remarks>
        /// This method can be used to provide localized binding to enums in ASP.NET applications.   Unlike 
        /// windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values
        /// to the displayed text.   You can bind an ASP.NET control to the list returned by this method by setting
        /// the DataValueField to "Key" and theDataTextField to "Value". 
        /// </remarks>
        static public List<KeyValuePair<Enum, string>> GetValues(Type enumType, CultureInfo culture)
        {
            List<KeyValuePair<Enum, string>> result = new List<KeyValuePair<Enum, string>>();
            TypeConverter converter = TypeDescriptor.GetConverter(enumType);
            foreach (Enum value in Enum.GetValues(enumType))
            {
                KeyValuePair<Enum, string> pair = new KeyValuePair<Enum, string>(value, converter.ConvertToString(null, culture, value));
                result.Add(pair); 
            }
            return result;
        }

        /// <summary>
        /// Return a list of the enum values and their associated display text for the given enum type in the current UI Culture
        /// </summary>
        /// <param name="enumType">The enum type to get the values for</param>
        /// <returns>
        /// A list of KeyValuePairs where the key is the enum value and the value is the text to display
        /// </returns>
        /// <remarks>
        /// This method can be used to provide localized binding to enums in ASP.NET applications.   Unlike 
        /// windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values
        /// to the displayed text.   You can bind an ASP.NET control to the list returned by this method by setting
        /// the DataValueField to "Key" and theDataTextField to "Value". 
        /// </remarks>
        static public List<KeyValuePair<Enum, string>> GetValues(Type enumType)
        {
            return GetValues(enumType, CultureInfo.CurrentUICulture);
        }
    }
}

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)

About the Author

Guillaume Waser
Architect A.C.S.
France France
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 22 Oct 2012
Article Copyright 2011 by Guillaume Waser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid