#region Imports using DPumpHydr.WinFrmUI.RLT.Design.Metro; using DPumpHydr.WinFrmUI.RLT.Enum.Metro; using DPumpHydr.WinFrmUI.RLT.Interface.Metro; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.IO; using System.Windows.Forms; using System.Windows.Forms.Design; using System.Xml; #endregion namespace DPumpHydr.WinFrmUI.RLT.Manager { #region MetroStyleManagerManager [DefaultProperty("Style")] [Designer(typeof(MetroStyleManagerDesigner))] [ToolboxItem(true)] [ToolboxBitmap(typeof(MetroStyleManager), "Style.bmp")] public class MetroStyleManager : Component { #region Constructor public MetroStyleManager(Form ownerForm) { OwnerForm = ownerForm; } public MetroStyleManager() { Style = Style.Light; if (_customTheme == null) { string themePath = Properties.Settings.Default.ThemeFile; if (File.Exists(themePath)) { FileInfo FI = new(themePath); if (FI.Length > 0) { _customTheme = themePath; } else { _customTheme = ThemeFilePath(Properties.Resources.Metro_Theme); } } else { _customTheme = ThemeFilePath(Properties.Resources.Metro_Theme); } } EvaluateDicts(); } #endregion #region Methods private void UpdateForm() { switch (OwnerForm) { case null: return; case IMetroForm form when CustomTheme == null: form.Style = Style; form.ThemeAuthor = ThemeAuthor; form.ThemeName = ThemeName; form.StyleManager = this; break; case IMetroForm form when CustomTheme != null: form.Style = Style; form.ThemeAuthor = ThemeAuthor; form.ThemeName = ThemeName; form.StyleManager = this; break; } if (OwnerForm.Controls.Count > 0) { UpdateControls(OwnerForm.Controls); } OwnerForm.Invalidate(); } private void UpdateControls(Control.ControlCollection controls) { if (controls == null) { throw new ArgumentNullException(nameof(controls)); } foreach (Control ctrl in controls) { IMetroControl control = ctrl as IMetroControl; if (control != null && (CustomTheme == null || CustomTheme != null)) { control.Style = Style; control.ThemeAuthor = ThemeAuthor; control.ThemeName = ThemeName; control.StyleManager = this; } if (control is TabControl tabControl) { foreach (TabPage c in tabControl.TabPages) { if (c is IMetroControl) { control.Style = Style; control.StyleManager = this; control.ThemeAuthor = ThemeAuthor; control.ThemeName = ThemeName; } UpdateControls(c.Controls); } } foreach (Control child in ctrl.Controls) { if (child is not IMetroControl) { continue; } ((IMetroControl)child).Style = Style; ((IMetroControl)child).StyleManager = this; ((IMetroControl)child).ThemeName = ThemeName; ((IMetroControl)child).ThemeAuthor = ThemeAuthor; } } } private void ControlAdded(object sender, ControlEventArgs e) { if (e.Control is IMetroControl control && CustomTheme != null) { control.Style = Style; control.ThemeAuthor = ThemeAuthor; control.ThemeName = ThemeName; control.StyleManager = this; } else { UpdateForm(); } } #endregion #region Internal Vars private Style _style; private Form _ownerForm; private string _customTheme; #endregion Internal Vars #region Properties [Category("Metro"), Description("Gets or sets the The Author name associated with the theme.")] public string ThemeAuthor { get; set; } [Category("Metro"), Description("Gets or sets the The Theme name associated with the theme.")] public string ThemeName { get; set; } [Category("Metro"), Description("Gets or sets the form (MetroForm) to Apply themes for.")] public Form OwnerForm { get => _ownerForm; set { if (_ownerForm != null) { return; } _ownerForm = value; _ownerForm.ControlAdded += ControlAdded; UpdateForm(); } } [Category("Metro"), Description("Gets or sets the style.")] public Style Style { get => _style; set { _style = value; switch (value) { case Style.Light: ThemeAuthor = "Taiizor"; ThemeName = "MetroLight"; break; case Style.Dark: ThemeAuthor = "Taiizor"; ThemeName = "MetroDark"; break; case Style.Custom: if (!string.IsNullOrEmpty(_customTheme) && File.Exists(_customTheme)) { Properties.Settings.Default.ThemeFile = _customTheme; Properties.Settings.Default.Save(); ControlProperties(_customTheme); } else { Style = Style.Light; } break; } UpdateForm(); } } [Editor(typeof(FileNamesEditor), typeof(UITypeEditor)), Category("Metro"), Description("Gets or sets the custom theme file.")] public string CustomTheme { get => _customTheme; set { if (!string.IsNullOrEmpty(value) && File.Exists(value)) { Properties.Settings.Default.ThemeFile = value; Properties.Settings.Default.Save(); ControlProperties(value); _customTheme = value; Style = Style.Custom; } else { _customTheme = null; Style = Style.Light; } } } #endregion Properties #region Open Theme public void OpenTheme() { using OpenFileDialog ofd = new() { Filter = @"Xml File (*.xml)|*.xml" }; if (ofd.ShowDialog() != DialogResult.OK) { return; } CustomTheme = ofd.FileName; } public void SetTheme(string path) { Style = Style.Custom; CustomTheme = path; } private static string ThemeFilePath(string str) { string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Templates), "ThemeFile.xml"); File.WriteAllText(path, str); return path; } #endregion Open Theme #region Dictionaries #region Declartions public Dictionary ButtonDictionary; public Dictionary DefaultButtonDictionary; public Dictionary LabelDictionary; public Dictionary LinkLabelDictionary; public Dictionary TextBoxDictionary; public Dictionary RichTextBoxDictionary; public Dictionary ComboBoxDictionary; public Dictionary FormDictionary; public Dictionary BadgeDictionary; public Dictionary DividerDictionary; public Dictionary CheckBoxDictionary; public Dictionary RadioButtonDictionary; public Dictionary SwitchBoxDictionary; public Dictionary ToolTipDictionary; public Dictionary NumericDictionary; public Dictionary EllipseDictionary; public Dictionary TileDictionary; public Dictionary ProgressDictionary; public Dictionary ControlBoxDictionary; public Dictionary TabControlDictionary; public Dictionary ScrollBarDictionary; public Dictionary PanelDictionary; public Dictionary TrackBarDictionary; public Dictionary ContextMenuDictionary; public Dictionary ListBoxDictionary; #endregion #region Methods private void Clear() { ButtonDictionary.Clear(); DefaultButtonDictionary.Clear(); FormDictionary.Clear(); LabelDictionary.Clear(); TextBoxDictionary.Clear(); LabelDictionary.Clear(); LinkLabelDictionary.Clear(); BadgeDictionary.Clear(); DividerDictionary.Clear(); CheckBoxDictionary.Clear(); RadioButtonDictionary.Clear(); SwitchBoxDictionary.Clear(); ToolTipDictionary.Clear(); RichTextBoxDictionary.Clear(); ComboBoxDictionary.Clear(); NumericDictionary.Clear(); EllipseDictionary.Clear(); TileDictionary.Clear(); ProgressDictionary.Clear(); ControlBoxDictionary.Clear(); TabControlDictionary.Clear(); ScrollBarDictionary.Clear(); PanelDictionary.Clear(); TrackBarDictionary.Clear(); ContextMenuDictionary.Clear(); ListBoxDictionary.Clear(); } #endregion #region Evaluate private void EvaluateDicts() { ButtonDictionary = new Dictionary(); DefaultButtonDictionary = new Dictionary(); LabelDictionary = new Dictionary(); LinkLabelDictionary = new Dictionary(); TextBoxDictionary = new Dictionary(); RichTextBoxDictionary = new Dictionary(); FormDictionary = new Dictionary(); BadgeDictionary = new Dictionary(); DividerDictionary = new Dictionary(); CheckBoxDictionary = new Dictionary(); RadioButtonDictionary = new Dictionary(); SwitchBoxDictionary = new Dictionary(); ToolTipDictionary = new Dictionary(); ComboBoxDictionary = new Dictionary(); NumericDictionary = new Dictionary(); EllipseDictionary = new Dictionary(); TileDictionary = new Dictionary(); ProgressDictionary = new Dictionary(); ControlBoxDictionary = new Dictionary(); TabControlDictionary = new Dictionary(); ScrollBarDictionary = new Dictionary(); PanelDictionary = new Dictionary(); TrackBarDictionary = new Dictionary(); ContextMenuDictionary = new Dictionary(); ListBoxDictionary = new Dictionary(); } #endregion #endregion #region Reader private void ControlProperties(string path) { Clear(); FormDictionary = GetValues(path, "Form"); ButtonDictionary = GetValues(path, "Button"); DefaultButtonDictionary = GetValues(path, "DefaultButton"); LabelDictionary = GetValues(path, "Label"); LinkLabelDictionary = GetValues(path, "LinkLabel"); BadgeDictionary = GetValues(path, "Badge"); DividerDictionary = GetValues(path, "Divider"); CheckBoxDictionary = GetValues(path, "CheckBox"); RadioButtonDictionary = GetValues(path, "RadioButton"); SwitchBoxDictionary = GetValues(path, "SwitchBox"); ToolTipDictionary = GetValues(path, "ToolTip"); TextBoxDictionary = GetValues(path, "TextBox"); RichTextBoxDictionary = GetValues(path, "RichTextBox"); ComboBoxDictionary = GetValues(path, "ComboBox"); NumericDictionary = GetValues(path, "Numeric"); EllipseDictionary = GetValues(path, "Ellipse"); TileDictionary = GetValues(path, "Tile"); ProgressDictionary = GetValues(path, "Progress"); ControlBoxDictionary = GetValues(path, "ControlBox"); TabControlDictionary = GetValues(path, "TabControl"); ScrollBarDictionary = GetValues(path, "ScrollBar"); PanelDictionary = GetValues(path, "Panel"); TrackBarDictionary = GetValues(path, "TrackBar"); ContextMenuDictionary = GetValues(path, "ContextMenu"); ListBoxDictionary = GetValues(path, "ListBox"); ThemeDetailsReader(path); UpdateForm(); } private void ThemeDetailsReader(string path) { try { foreach (KeyValuePair item in GetValues(path, "Theme")) { if (item.Key == "Name") { ThemeName = item.Value.ToString(); } else if (item.Key == "Author") { ThemeAuthor = item.Value.ToString(); } } } catch { return; } } private static Dictionary GetValues(string path, string nodename) { try { Dictionary dict = new(); XmlDocument doc = new(); if (File.Exists(path)) { doc.Load(path); } if (doc.DocumentElement == null) { return null; } XmlNode xmlNode = doc.SelectSingleNode($"/MetroTheme/{nodename}"); if (xmlNode == null) { return dict; } foreach (XmlNode node in xmlNode.ChildNodes) { dict.Add(node.Name, node.InnerText); } return dict; } catch { return new(); } } #endregion Reader #region UITypeEditor public class FileNamesEditor : UITypeEditor { private OpenFileDialog _ofd; public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { if (context == null || provider == null) { return base.EditValue(context, provider, value); } IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (editorService == null) { return base.EditValue(context, provider, value); } _ofd = new OpenFileDialog { Filter = @"Xml File (*.xml)|*.xml", }; return _ofd.ShowDialog() == DialogResult.OK ? _ofd.FileName : base.EditValue(context, provider, value); } } #endregion } #endregion }