Server/DisplaySettings.cs
author StephaneLenclud
Sat, 07 Feb 2015 13:51:30 +0100
changeset 109 ff1ed5f07d7e
parent 100 7e02ac8b13ba
child 115 5c61a13c4241
permissions -rw-r--r--
MiniDisplay: Improved icon APIs.
sl@49
     1
using System;
sl@49
     2
using System.Collections;
sl@49
     3
using System.Collections.Generic;
sl@49
     4
using System.Text;
sl@49
     5
using System.Configuration;
sl@49
     6
using System.Xml;
sl@49
     7
using System.ComponentModel;
sl@49
     8
using System.Runtime.Serialization;
sl@49
     9
using System.Runtime.Serialization.Json;
sl@49
    10
using System.IO;
sl@49
    11
using System.Drawing;
sl@49
    12
sl@49
    13
namespace SharpDisplayManager
sl@49
    14
{
sl@49
    15
    /// <summary>
sl@50
    16
    /// Display settings for the specified hardware type
sl@49
    17
    /// </summary>
sl@49
    18
    [DataContract]
sl@50
    19
    public class DisplaySettings
sl@49
    20
    {
sl@50
    21
        public DisplaySettings()
sl@49
    22
        {
sl@49
    23
            Brightness = 1;
sl@49
    24
            DisplayType = 0;
sl@49
    25
            TimerInterval = 150;
sl@49
    26
            ReverseScreen = false;
sl@57
    27
            InverseColors = true;
sl@49
    28
            ShowBorders = false;
sl@49
    29
            FontName = "Microsoft Sans Serif, 9.75pt";
sl@100
    30
            ScaleToFit = true;
sl@100
    31
            MinFontSize = 15.0f;
StephaneLenclud@106
    32
            Separator = "   ";
StephaneLenclud@106
    33
			ScrollingSpeedInPixelsPerSecond = 64;
sl@49
    34
        }
sl@49
    35
sl@49
    36
        [DataMember]
sl@49
    37
        public int Brightness { get; set; }
sl@49
    38
sl@50
    39
        /// <summary>
sl@50
    40
        /// See Display.TMiniDisplayType
sl@50
    41
        /// </summary>
sl@49
    42
        [DataMember]
sl@49
    43
        public int DisplayType { get; set; }
sl@49
    44
sl@49
    45
        [DataMember]
sl@49
    46
        public int TimerInterval { get; set; }
sl@49
    47
StephaneLenclud@106
    48
		[DataMember]
StephaneLenclud@106
    49
		public int ScrollingSpeedInPixelsPerSecond { get; set; }
StephaneLenclud@106
    50
sl@49
    51
        [DataMember]
sl@49
    52
        public bool ReverseScreen { get; set; }
sl@49
    53
sl@49
    54
        [DataMember]
sl@57
    55
        public bool InverseColors { get; set; }
sl@57
    56
sl@57
    57
        [DataMember]
sl@49
    58
        public bool ShowBorders { get; set; }
sl@49
    59
sl@49
    60
        [DataMember]
sl@100
    61
        public bool ScaleToFit { get; set; }
sl@100
    62
sl@100
    63
        [DataMember]
sl@100
    64
        public float MinFontSize { get; set; }
sl@100
    65
sl@100
    66
        [DataMember]
sl@100
    67
        public string Separator { get; set; }
sl@100
    68
sl@100
    69
        [DataMember]
sl@49
    70
        public string FontName { get; set; }
sl@49
    71
sl@49
    72
        public Font Font
sl@49
    73
        {
sl@49
    74
            get
sl@49
    75
            {
sl@49
    76
                FontConverter cvt = new FontConverter();
sl@49
    77
                Font font = cvt.ConvertFromInvariantString(FontName) as Font;
sl@49
    78
                return font;
sl@49
    79
            }
sl@49
    80
sl@49
    81
            set
sl@49
    82
            {
sl@49
    83
                FontConverter cvt = new FontConverter();
sl@49
    84
                FontName = cvt.ConvertToInvariantString(value);
sl@49
    85
            }
sl@49
    86
        }
sl@49
    87
    };
sl@49
    88
sl@49
    89
sl@50
    90
    /// <summary>
sl@100
    91
    /// Contain settings for each of our display type.
sl@50
    92
    /// </summary>
sl@49
    93
    [TypeConverter(typeof(DisplaySettingsConverter))]
sl@49
    94
    [DataContract]
sl@50
    95
    public class DisplaysSettings
sl@49
    96
    {
sl@50
    97
        public DisplaysSettings()
sl@49
    98
        {
sl@49
    99
            Init();
sl@49
   100
        }
sl@49
   101
sl@49
   102
        public void Init()
sl@49
   103
        {
sl@49
   104
            if (Displays == null)
sl@49
   105
            {
sl@50
   106
                Displays = new List<DisplaySettings>();
sl@49
   107
            }
sl@49
   108
        }
sl@49
   109
sl@49
   110
        //[DataMember]
sl@49
   111
        //public int CurrentSettingsIndex { get; set; }
sl@49
   112
sl@49
   113
        [DataMember]
sl@50
   114
        public List<DisplaySettings> Displays { get; set; }
sl@49
   115
sl@49
   116
        public override string ToString()
sl@49
   117
        {
sl@49
   118
            //Save settings into JSON string
sl@49
   119
            MemoryStream stream = new MemoryStream();
sl@50
   120
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@49
   121
            ser.WriteObject(stream, this);
sl@49
   122
            // convert stream to string
sl@49
   123
            stream.Position = 0;
sl@49
   124
            StreamReader reader = new StreamReader(stream);
sl@49
   125
            string text = reader.ReadToEnd();
sl@49
   126
            return text;
sl@49
   127
        }
sl@49
   128
    }
sl@49
   129
sl@49
   130
    public class DisplaySettingsConverter : TypeConverter
sl@49
   131
    {
sl@49
   132
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
sl@49
   133
        {
sl@49
   134
            if (sourceType == typeof(string))
sl@49
   135
                return true;
sl@49
   136
            else
sl@49
   137
                return base.CanConvertFrom(context, sourceType);
sl@49
   138
        }
sl@49
   139
sl@49
   140
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
sl@49
   141
        {
sl@49
   142
            string stringValue = value as string;
sl@49
   143
            if (stringValue != null)
sl@100
   144
            {
sl@49
   145
                //Load settings form JSON string
sl@49
   146
                byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
sl@49
   147
                MemoryStream stream = new MemoryStream(byteArray);
sl@50
   148
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@50
   149
                DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
sl@49
   150
                settings.Init();
sl@49
   151
                return settings;
sl@49
   152
            }
sl@49
   153
            else
sl@49
   154
                return base.ConvertFrom(context, culture, value);
sl@49
   155
        }
sl@49
   156
    };
sl@49
   157
sl@49
   158
sl@49
   159
}
sl@49
   160