Server/DisplaySettings.cs
author StephaneLenclud
Mon, 09 Feb 2015 22:47:13 +0100
changeset 120 300f44b7f0cc
parent 106 32270ff62819
child 123 0df386e37e29
permissions -rw-r--r--
Fixing div by zero in network signal animation code of GP1212 displays.
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;
StephaneLenclud@115
    29
			ShowVolumeLabel = false;
sl@49
    30
            FontName = "Microsoft Sans Serif, 9.75pt";
sl@100
    31
            ScaleToFit = true;
sl@100
    32
            MinFontSize = 15.0f;
StephaneLenclud@106
    33
            Separator = "   ";
StephaneLenclud@106
    34
			ScrollingSpeedInPixelsPerSecond = 64;
sl@49
    35
        }
sl@49
    36
StephaneLenclud@115
    37
StephaneLenclud@115
    38
		[DataMember]
StephaneLenclud@115
    39
		public bool ShowVolumeLabel { get; set; }
StephaneLenclud@115
    40
sl@49
    41
        [DataMember]
sl@49
    42
        public int Brightness { get; set; }
sl@49
    43
sl@50
    44
        /// <summary>
sl@50
    45
        /// See Display.TMiniDisplayType
sl@50
    46
        /// </summary>
sl@49
    47
        [DataMember]
sl@49
    48
        public int DisplayType { get; set; }
sl@49
    49
sl@49
    50
        [DataMember]
sl@49
    51
        public int TimerInterval { get; set; }
sl@49
    52
StephaneLenclud@106
    53
		[DataMember]
StephaneLenclud@106
    54
		public int ScrollingSpeedInPixelsPerSecond { get; set; }
StephaneLenclud@106
    55
sl@49
    56
        [DataMember]
sl@49
    57
        public bool ReverseScreen { get; set; }
sl@49
    58
sl@49
    59
        [DataMember]
sl@57
    60
        public bool InverseColors { get; set; }
sl@57
    61
sl@57
    62
        [DataMember]
sl@49
    63
        public bool ShowBorders { get; set; }
sl@49
    64
sl@49
    65
        [DataMember]
sl@100
    66
        public bool ScaleToFit { get; set; }
sl@100
    67
sl@100
    68
        [DataMember]
sl@100
    69
        public float MinFontSize { get; set; }
sl@100
    70
sl@100
    71
        [DataMember]
sl@100
    72
        public string Separator { get; set; }
sl@100
    73
sl@100
    74
        [DataMember]
sl@49
    75
        public string FontName { get; set; }
sl@49
    76
sl@49
    77
        public Font Font
sl@49
    78
        {
sl@49
    79
            get
sl@49
    80
            {
sl@49
    81
                FontConverter cvt = new FontConverter();
sl@49
    82
                Font font = cvt.ConvertFromInvariantString(FontName) as Font;
sl@49
    83
                return font;
sl@49
    84
            }
sl@49
    85
sl@49
    86
            set
sl@49
    87
            {
sl@49
    88
                FontConverter cvt = new FontConverter();
sl@49
    89
                FontName = cvt.ConvertToInvariantString(value);
sl@49
    90
            }
sl@49
    91
        }
sl@49
    92
    };
sl@49
    93
sl@49
    94
sl@50
    95
    /// <summary>
sl@100
    96
    /// Contain settings for each of our display type.
sl@50
    97
    /// </summary>
sl@49
    98
    [TypeConverter(typeof(DisplaySettingsConverter))]
sl@49
    99
    [DataContract]
sl@50
   100
    public class DisplaysSettings
sl@49
   101
    {
sl@50
   102
        public DisplaysSettings()
sl@49
   103
        {
sl@49
   104
            Init();
sl@49
   105
        }
sl@49
   106
sl@49
   107
        public void Init()
sl@49
   108
        {
sl@49
   109
            if (Displays == null)
sl@49
   110
            {
sl@50
   111
                Displays = new List<DisplaySettings>();
sl@49
   112
            }
sl@49
   113
        }
sl@49
   114
sl@49
   115
        //[DataMember]
sl@49
   116
        //public int CurrentSettingsIndex { get; set; }
sl@49
   117
sl@49
   118
        [DataMember]
sl@50
   119
        public List<DisplaySettings> Displays { get; set; }
sl@49
   120
sl@49
   121
        public override string ToString()
sl@49
   122
        {
sl@49
   123
            //Save settings into JSON string
sl@49
   124
            MemoryStream stream = new MemoryStream();
sl@50
   125
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@49
   126
            ser.WriteObject(stream, this);
sl@49
   127
            // convert stream to string
sl@49
   128
            stream.Position = 0;
sl@49
   129
            StreamReader reader = new StreamReader(stream);
sl@49
   130
            string text = reader.ReadToEnd();
sl@49
   131
            return text;
sl@49
   132
        }
sl@49
   133
    }
sl@49
   134
sl@49
   135
    public class DisplaySettingsConverter : TypeConverter
sl@49
   136
    {
sl@49
   137
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
sl@49
   138
        {
sl@49
   139
            if (sourceType == typeof(string))
sl@49
   140
                return true;
sl@49
   141
            else
sl@49
   142
                return base.CanConvertFrom(context, sourceType);
sl@49
   143
        }
sl@49
   144
sl@49
   145
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
sl@49
   146
        {
sl@49
   147
            string stringValue = value as string;
sl@49
   148
            if (stringValue != null)
sl@100
   149
            {
sl@49
   150
                //Load settings form JSON string
sl@49
   151
                byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
sl@49
   152
                MemoryStream stream = new MemoryStream(byteArray);
sl@50
   153
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@50
   154
                DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
sl@49
   155
                settings.Init();
sl@49
   156
                return settings;
sl@49
   157
            }
sl@49
   158
            else
sl@49
   159
                return base.ConvertFrom(context, culture, value);
sl@49
   160
        }
sl@49
   161
    };
sl@49
   162
sl@49
   163
sl@49
   164
}
sl@49
   165