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