/[NexusPowerControl]/trunk/NexusPowerControl/ThemeLoader.cs
ViewVC logotype

Annotation of /trunk/NexusPowerControl/ThemeLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide annotations) (download)
Tue Oct 25 07:13:09 2011 UTC (8 years ago) by william
File size: 7492 byte(s)
*** Fully implement default theme and AeroLowRes theme buttons

1 william 8 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5 william 9 using System.IO;
6     using System.Xml;
7 william 8
8     namespace NexusPowerControl
9     {
10    
11    
12    
13     public interface IThemeLoader
14     {
15     bool LoadThemeOrDefault(string ThemeName);
16     //bool LoadCustomTheme(string ThemeName, string ThemeXml);
17     ThemeLoader.ThemeDefinition GetLoadedTheme();
18     }
19     public class ThemeLoader : IThemeLoader
20     {
21    
22     private ThemeLoader.ThemeDefinition LoadedTheme;
23     public ThemeLoader()
24     {
25     this.LoadedTheme = new ThemeDefinition();
26     this.LoadThemeOrDefault(ThemeDefinition.DefaultTheme);
27     }
28     #region IThemeLoader Members
29     public bool LoadThemeOrDefault(string ThemeName)
30     {
31     this.LoadedTheme = new ThemeDefinition(ThemeName);
32     return LoadedTheme.ThemeLoaded;
33     }
34     public ThemeLoader.ThemeDefinition GetLoadedTheme()
35     {
36     return (this.LoadedTheme == null) ? new ThemeDefinition() : this.LoadedTheme;
37     }
38     #endregion
39    
40     #region sub-classes
41 william 9 public class ThemeElements
42 william 8 {
43 william 9 public const string main = "main";
44     public const string shutdown = "shutdown";
45     public const string restart = "restart";
46     public const string logoff = "logoff";
47     public const string lockscreen = "lockscreen";
48     public const string sleep = "sleep";
49     public const string close = "close";
50     }
51     public interface IThemeDefinition
52     {
53 william 21 bool ThemeLoaded { get; }
54 william 9 string MainImage { get; set; }
55     string ShutdownImage { get; set; }
56     string RestartImage { get; set; }
57     string LogoffImage { get; set; }
58     string LockScreenImage { get; set; }
59     string SleepImage { get; set; }
60     string CloseImage { get; set; }
61     }
62     public class ThemeDefinition : IThemeDefinition
63     {
64 william 21 //private const string MissingImage = "missing.png";
65     //private const string ThemeDirectory = "theme";
66     //private const string ThemeFile = "theme.xml";
67 william 8
68     public const string DefaultTheme = "AeroLowRes";
69     public ThemeDefinition() : this(ThemeDefinition.DefaultTheme) { }
70 william 9 public ThemeDefinition(string ThemeXml) { this.init(); this.LoadTheme(ThemeXml); }
71 william 8
72 william 9 private void init()
73     {
74 william 21 //this.ThemeLoaded = false;
75 william 9 this.MainImage = "";
76     this.ShutdownImage = "";
77     this.RestartImage = "";
78     this.LogoffImage = "";
79     this.LockScreenImage = "";
80     this.SleepImage = "";
81     this.CloseImage = "";
82     }
83    
84 william 8 private void LoadTheme(string theme) { if (theme.ToLower().Contains(".xml")) { this.LoadCustomTheme(theme); } else { this.LoadStandardTheme(theme); } }
85    
86     private void LoadStandardTheme(string theme)
87     {
88     // load theme in format: theme/<themname>/theme.xml : will throw file not found excetion (use default)
89 william 21 string xml_file = string.Format(@"{0}\{1}\{2}", ThemeConstants.ThemeDirectory, theme, ThemeConstants.ThemeFile);
90 william 9 this.LoadCustomTheme(xml_file);
91 william 8 }
92     private void LoadCustomTheme(string theme)
93     {
94     // load theme in format: <theme xml file>: will throw file not found excetion (use default)
95 william 21 try { if (!File.Exists(theme)) { throw new FileNotFoundException(theme); } else { try { this.ReadThemeXml(theme); } catch { return; } } }
96     catch (FileNotFoundException) { }//this.ThemeLoaded = false; }
97     catch (Exception) { }//this.ThemeLoaded = false; }
98 william 8 }
99 william 9
100     private void ReadThemeXml(string theme_xml)
101     {
102     FileStream fs = new FileStream(theme_xml, FileMode.Open, FileAccess.Read);
103     XmlReader r = XmlReader.Create(fs);
104     while (r.Read())
105     {
106     if (r.NodeType == XmlNodeType.Element)
107     {
108     if (r.Name.ToLower() == "theme") { r.Read(); }
109    
110     if (r.HasAttributes)
111     {
112     switch (r.Name.ToLower())
113     {
114     case ThemeElements.close: this.CloseImage = BuildThemeImageURI(theme_xml,r.GetAttribute("value")); break;
115     case ThemeElements.lockscreen: this.LockScreenImage =BuildThemeImageURI(theme_xml,r.GetAttribute("value")); break;
116     case ThemeElements.logoff: this.LogoffImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
117     case ThemeElements.main: this.MainImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
118     case ThemeElements.restart: this.RestartImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
119     case ThemeElements.shutdown: this.ShutdownImage =BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
120     case ThemeElements.sleep: this.SleepImage = BuildThemeImageURI(theme_xml, r.GetAttribute("value")); break;
121 william 21 //default: this.ThemeLoaded = false; break;
122 william 9 }
123     }
124     }
125     }
126     }
127    
128     private string BuildThemeImageURI(string theme_xml, string image)
129     {
130     FileInfo fs = new FileInfo(theme_xml);
131     string path = fs.Directory.FullName;
132     string fullpath = string.Format(@"{0}\{1}", path, image);
133     fs = new FileInfo(fullpath);
134     if (!fs.Exists)
135     {
136 william 21 //fs = new FileInfo(ThemeConstants.ThemeDirectory);
137     //fullpath = string.Format(@"{0}\{1}\{2}", fs.DirectoryName, ThemeConstants.ThemeDirectory, MissingImage);
138     fullpath = "";
139     //this.ThemeLoaded = false;
140 william 9 }
141     return fullpath;
142     }
143    
144     #region IThemeDefinition Members
145 william 21 public bool ThemeLoaded {
146     get
147     {
148     return (
149     this.MainImage != "" &&
150     this.ShutdownImage != "" &&
151     this.RestartImage != "" &&
152     this.LogoffImage != "" &&
153     this.LockScreenImage != "" &&
154     this.SleepImage != "" &&
155     this.CloseImage != "");
156     }
157     }
158 william 9 public string MainImage { get; set; }
159     public string ShutdownImage { get; set; }
160     public string RestartImage { get; set; }
161     public string LogoffImage { get; set; }
162     public string LockScreenImage { get; set; }
163     public string SleepImage { get; set; }
164     public string CloseImage { get; set; }
165     #endregion
166 william 8 }
167     #endregion
168     }
169     }

  ViewVC Help
Powered by ViewVC 1.1.22