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