Server/DisplaySettings.cs
author StephaneLenclud
Thu, 21 Jan 2016 15:00:05 +0100
changeset 178 9eb93d079a3e
parent 115 5c61a13c4241
child 212 1a0791daa243
permissions -rw-r--r--
Simplify GetIcon function.
StephaneLenclud@123
     1
//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
sl@49
    21
using System.Collections;
sl@49
    22
using System.Collections.Generic;
sl@49
    23
using System.Text;
sl@49
    24
using System.Configuration;
sl@49
    25
using System.Xml;
sl@49
    26
using System.ComponentModel;
sl@49
    27
using System.Runtime.Serialization;
sl@49
    28
using System.Runtime.Serialization.Json;
sl@49
    29
using System.IO;
sl@49
    30
using System.Drawing;
sl@49
    31
sl@49
    32
namespace SharpDisplayManager
sl@49
    33
{
sl@49
    34
    /// <summary>
sl@50
    35
    /// Display settings for the specified hardware type
sl@49
    36
    /// </summary>
sl@49
    37
    [DataContract]
sl@50
    38
    public class DisplaySettings
sl@49
    39
    {
sl@50
    40
        public DisplaySettings()
sl@49
    41
        {
sl@49
    42
            Brightness = 1;
sl@49
    43
            DisplayType = 0;
sl@49
    44
            TimerInterval = 150;
sl@49
    45
            ReverseScreen = false;
sl@57
    46
            InverseColors = true;
sl@49
    47
            ShowBorders = false;
StephaneLenclud@115
    48
			ShowVolumeLabel = false;
sl@49
    49
            FontName = "Microsoft Sans Serif, 9.75pt";
sl@100
    50
            ScaleToFit = true;
sl@100
    51
            MinFontSize = 15.0f;
StephaneLenclud@106
    52
            Separator = "   ";
StephaneLenclud@106
    53
			ScrollingSpeedInPixelsPerSecond = 64;
sl@49
    54
        }
sl@49
    55
StephaneLenclud@115
    56
StephaneLenclud@115
    57
		[DataMember]
StephaneLenclud@115
    58
		public bool ShowVolumeLabel { get; set; }
StephaneLenclud@115
    59
sl@49
    60
        [DataMember]
sl@49
    61
        public int Brightness { get; set; }
sl@49
    62
sl@50
    63
        /// <summary>
sl@50
    64
        /// See Display.TMiniDisplayType
sl@50
    65
        /// </summary>
sl@49
    66
        [DataMember]
sl@49
    67
        public int DisplayType { get; set; }
sl@49
    68
sl@49
    69
        [DataMember]
sl@49
    70
        public int TimerInterval { get; set; }
sl@49
    71
StephaneLenclud@106
    72
		[DataMember]
StephaneLenclud@106
    73
		public int ScrollingSpeedInPixelsPerSecond { get; set; }
StephaneLenclud@106
    74
sl@49
    75
        [DataMember]
sl@49
    76
        public bool ReverseScreen { get; set; }
sl@49
    77
sl@49
    78
        [DataMember]
sl@57
    79
        public bool InverseColors { get; set; }
sl@57
    80
sl@57
    81
        [DataMember]
sl@49
    82
        public bool ShowBorders { get; set; }
sl@49
    83
sl@49
    84
        [DataMember]
sl@100
    85
        public bool ScaleToFit { get; set; }
sl@100
    86
sl@100
    87
        [DataMember]
sl@100
    88
        public float MinFontSize { get; set; }
sl@100
    89
sl@100
    90
        [DataMember]
sl@100
    91
        public string Separator { get; set; }
sl@100
    92
sl@100
    93
        [DataMember]
sl@49
    94
        public string FontName { get; set; }
sl@49
    95
sl@49
    96
        public Font Font
sl@49
    97
        {
sl@49
    98
            get
sl@49
    99
            {
sl@49
   100
                FontConverter cvt = new FontConverter();
sl@49
   101
                Font font = cvt.ConvertFromInvariantString(FontName) as Font;
sl@49
   102
                return font;
sl@49
   103
            }
sl@49
   104
sl@49
   105
            set
sl@49
   106
            {
sl@49
   107
                FontConverter cvt = new FontConverter();
sl@49
   108
                FontName = cvt.ConvertToInvariantString(value);
sl@49
   109
            }
sl@49
   110
        }
sl@49
   111
    };
sl@49
   112
sl@49
   113
sl@50
   114
    /// <summary>
sl@100
   115
    /// Contain settings for each of our display type.
sl@50
   116
    /// </summary>
sl@49
   117
    [TypeConverter(typeof(DisplaySettingsConverter))]
sl@49
   118
    [DataContract]
sl@50
   119
    public class DisplaysSettings
sl@49
   120
    {
sl@50
   121
        public DisplaysSettings()
sl@49
   122
        {
sl@49
   123
            Init();
sl@49
   124
        }
sl@49
   125
sl@49
   126
        public void Init()
sl@49
   127
        {
sl@49
   128
            if (Displays == null)
sl@49
   129
            {
sl@50
   130
                Displays = new List<DisplaySettings>();
sl@49
   131
            }
sl@49
   132
        }
sl@49
   133
sl@49
   134
        //[DataMember]
sl@49
   135
        //public int CurrentSettingsIndex { get; set; }
sl@49
   136
sl@49
   137
        [DataMember]
sl@50
   138
        public List<DisplaySettings> Displays { get; set; }
sl@49
   139
sl@49
   140
        public override string ToString()
sl@49
   141
        {
sl@49
   142
            //Save settings into JSON string
sl@49
   143
            MemoryStream stream = new MemoryStream();
sl@50
   144
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@49
   145
            ser.WriteObject(stream, this);
sl@49
   146
            // convert stream to string
sl@49
   147
            stream.Position = 0;
sl@49
   148
            StreamReader reader = new StreamReader(stream);
sl@49
   149
            string text = reader.ReadToEnd();
sl@49
   150
            return text;
sl@49
   151
        }
sl@49
   152
    }
sl@49
   153
sl@49
   154
    public class DisplaySettingsConverter : TypeConverter
sl@49
   155
    {
sl@49
   156
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
sl@49
   157
        {
sl@49
   158
            if (sourceType == typeof(string))
sl@49
   159
                return true;
sl@49
   160
            else
sl@49
   161
                return base.CanConvertFrom(context, sourceType);
sl@49
   162
        }
sl@49
   163
sl@49
   164
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
sl@49
   165
        {
sl@49
   166
            string stringValue = value as string;
sl@49
   167
            if (stringValue != null)
sl@100
   168
            {
sl@49
   169
                //Load settings form JSON string
sl@49
   170
                byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
sl@49
   171
                MemoryStream stream = new MemoryStream(byteArray);
sl@50
   172
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
sl@50
   173
                DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
sl@49
   174
                settings.Init();
sl@49
   175
                return settings;
sl@49
   176
            }
sl@49
   177
            else
sl@49
   178
                return base.ConvertFrom(context, culture, value);
sl@49
   179
        }
sl@49
   180
    };
sl@49
   181
sl@49
   182
sl@49
   183
}
sl@49
   184