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