<commit_before>﻿using libmsstyle;
using msstyleEditor.Dialogs;
using msstyleEditor.Properties;
using msstyleEditor.PropView;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace msstyleEditor
{
    public partial class MainWindow : Form
    {
        private VisualStyle m_style;
        private Selection m_selection = new Selection();
        private StyleResource m_selectedImage = null;
        private ThemeManager m_themeManager = null;

        private SearchDialog m_searchDialog = new SearchDialog();
        private ClassViewWindow m_classView;
        private PropertyViewWindow m_propertyView;
        private ImageView m_imageView;
        private RenderView m_renderView;

        public MainWindow()
        {
            InitializeComponent();

            dockPanel.Theme = new WeifenLuo.WinFormsUI.Docking.VS2015LightTheme();
            //dockPanel.Theme.ColorPalette.ToolWindowCaptionActive.Background = SystemColors.ActiveCaption;

            m_classView = new ClassViewWindow();
            m_classView.Show(dockPanel, DockState.DockLeft);
            m_classView.CloseButtonVisible = false;
            m_classView.OnSelectionChanged += OnTreeItemSelected;
            m_classView.Controls[0].KeyPress += OnTreeKeyPress; // very hacky

            m_imageView = new ImageView();
            m_imageView.SelectedIndexChanged += OnImageSelectIndex;
            m_imageView.OnViewBackColorChanged += OnImageViewBackgroundChanged;
            m_imageView.Show(dockPanel, DockState.Document);
            m_imageView.VisibleChanged += (s, e) => { btShowImageView.Checked = m_imageView.Visible; };
            m_imageView.SetActiveTabs(-1, 0);

            m_renderView = new RenderView();
            m_renderView.Show(m_imageView.Pane, DockAlignment.Top, 0.25);
            m_renderView.VisibleChanged += (s, e) => { btShowRenderView.Checked = m_renderView.Visible; };
            m_renderView.Visible = false;
            m_renderView.Hide();

            m_propertyView = new PropertyViewWindow();
            m_propertyView.Show(dockPanel, DockState.DockRight);
            m_propertyView.CloseButtonVisible = false;
            m_propertyView.OnPropertyAdded += OnPropertyAdded;
            m_propertyView.OnPropertyRemoved += OnPropertyRemoved;

            try 
            {
                m_themeManager = new ThemeManager();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIs Aero/DWM disabled? Starting without \"Test Theme\" feature.", 
                    "Themeing API unavailable", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Warning);
                btTestTheme.Enabled = false;
            }

            SetTestThemeButtonState(false);
            CloseStyle();

            m_searchDialog.StartPosition = FormStartPosition.CenterParent;
            m_searchDialog.OnSearch += this.OnSearchNextItem;
        }

        #region State Management & Helper

        private void SetTestThemeButtonState(bool active)
        {
            if (active)
            {
                btTestTheme.LargeImage = Resources.Stop32;
                btTestTheme.Text = "Stop Test";
            }
            else
            {
                btTestTheme.LargeImage = Resources.Play32;
                btTestTheme.Text = "Test";
            }
        }

        private void UpdateItemSelection(StyleClass cls = null, StylePart part = null, StyleState state = null, int resId = -1)
        {
            m_selection.Class = cls;
            m_selection.Part = part;
            m_selection.State = state;
            m_selection.ResourceId = resId;

            lbStatusMessage.Text = "C: " + cls.ClassId;
            if (part != null) lbStatusMessage.Text += ", P: " + part.PartId;
            if (state != null) lbStatusMessage.Text += ", S: " + state.StateId;
            if (resId >= 0) lbStatusMessage.Text += ", R: " + resId;
        }

        private void UpdateWindowCaption(string text)
        {
            this.Text = "msstyleEditor - " + text;
        }

        private void UpdateStatusText(string text)
        {
            lbStatusMessage.Text = text;
        }

        private void UpdateImageInfo(Image img)
        {
            if(img == null)
            {
                lbImageInfo.Visible = false;
            }
            else
            {
                lbImageInfo.Text = $"{img.Width}x{img.Height}px";
                lbImageInfo.Visible = true;
            }
        }

        private void OpenStyle(string path)
        {
            try
            {
                m_style = new VisualStyle();
                m_style.Load(path);
                UpdateWindowCaption(path);
            }
            catch(Exception ex)
            {
                MessageBox.Show(this, $"Are you sure this is a Windows Vista or higher visual style?\r\n\r\nDetails: {ex.Message}", "Error loading style!", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
                return;
            }

            m_classView.SetVisualStyle(m_style);

            btFileSave.Enabled = true;
            btFileInfoExport.Enabled = true;
            btImageExport.Enabled = false;
            btImageImport.Enabled = false;
            //btPropertyExport.Enabled = false;
            //btPropertyImport.Enabled = false;
            btPropertyAdd.Enabled = true;
            btPropertyRemove.Enabled = true;
            btTestTheme.Enabled = true;

            if (m_style.StringTable?.Count == 0)
                btFileSave.Style = RibbonButtonStyle.Normal;
            else btFileSave.Style = RibbonButtonStyle.SplitDropDown;

            lbStylePlatform.Text = m_style.Platform.ToDisplayString();
        }

        private void CloseStyle()
        {
            m_classView.SetVisualStyle(null);
            m_propertyView.SetStylePart(null, null, null);

            btFileSave.Enabled = false;
            btFileInfoExport.Enabled = false;
            btImageExport.Enabled = false;
            btImageImport.Enabled = false;
            btPropertyExport.Enabled = false;
            btPropertyImport.Enabled = false;
            btPropertyAdd.Enabled = false;
            btPropertyRemove.Enabled = false;
            btTestTheme.Enabled = false;

            lbStylePlatform.Text = "";
            m_style?.Dispose();
        }

        private void DisplayClass(StyleClass cls)
        {
            // remember the shown class
            UpdateItemSelection(cls);
            // reset propery view
            m_propertyView.SetStylePart(m_style, cls, null);
            // reset image view
            m_selectedImage = UpdateImageView(null);
            // reset image selector<commit_msg>warn when the string table wasn't found (#91)<commit_after>﻿using libmsstyle;
using msstyleEditor.Dialogs;
using msstyleEditor.Properties;
using msstyleEditor.PropView;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace msstyleEditor
{
    public partial class MainWindow : Form
    {
        private VisualStyle m_style;
        private Selection m_selection = new Selection();
        private StyleResource m_selectedImage = null;
        private ThemeManager m_themeManager = null;

        private SearchDialog m_searchDialog = new SearchDialog();
        private ClassViewWindow m_classView;
        private PropertyViewWindow m_propertyView;
        private ImageView m_imageView;
        private RenderView m_renderView;

        public MainWindow()
        {
            InitializeComponent();

            dockPanel.Theme = new WeifenLuo.WinFormsUI.Docking.VS2015LightTheme();
            //dockPanel.Theme.ColorPalette.ToolWindowCaptionActive.Background = SystemColors.ActiveCaption;

            m_classView = new ClassViewWindow();
            m_classView.Show(dockPanel, DockState.DockLeft);
            m_classView.CloseButtonVisible = false;
            m_classView.OnSelectionChanged += OnTreeItemSelected;
            m_classView.Controls[0].KeyPress += OnTreeKeyPress; // very hacky

            m_imageView = new ImageView();
            m_imageView.SelectedIndexChanged += OnImageSelectIndex;
            m_imageView.OnViewBackColorChanged += OnImageViewBackgroundChanged;
            m_imageView.Show(dockPanel, DockState.Document);
            m_imageView.VisibleChanged += (s, e) => { btShowImageView.Checked = m_imageView.Visible; };
            m_imageView.SetActiveTabs(-1, 0);

            m_renderView = new RenderView();
            m_renderView.Show(m_imageView.Pane, DockAlignment.Top, 0.25);
            m_renderView.VisibleChanged += (s, e) => { btShowRenderView.Checked = m_renderView.Visible; };
            m_renderView.Visible = false;
            m_renderView.Hide();

            m_propertyView = new PropertyViewWindow();
            m_propertyView.Show(dockPanel, DockState.DockRight);
            m_propertyView.CloseButtonVisible = false;
            m_propertyView.OnPropertyAdded += OnPropertyAdded;
            m_propertyView.OnPropertyRemoved += OnPropertyRemoved;

            try 
            {
                m_themeManager = new ThemeManager();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIs Aero/DWM disabled? Starting without \"Test Theme\" feature.", 
                    "Themeing API unavailable", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Warning);
                btTestTheme.Enabled = false;
            }

            SetTestThemeButtonState(false);
            CloseStyle();

            m_searchDialog.StartPosition = FormStartPosition.CenterParent;
            m_searchDialog.OnSearch += this.OnSearchNextItem;
        }

        #region State Management & Helper

        private void SetTestThemeButtonState(bool active)
        {
            if (active)
            {
                btTestTheme.LargeImage = Resources.Stop32;
                btTestTheme.Text = "Stop Test";
            }
            else
            {
                btTestTheme.LargeImage = Resources.Play32;
                btTestTheme.Text = "Test";
            }
        }

        private void UpdateItemSelection(StyleClass cls = null, StylePart part = null, StyleState state = null, int resId = -1)
        {
            m_selection.Class = cls;
            m_selection.Part = part;
            m_selection.State = state;
            m_selection.ResourceId = resId;

            lbStatusMessage.Text = "C: " + cls.ClassId;
            if (part != null) lbStatusMessage.Text += ", P: " + part.PartId;
            if (state != null) lbStatusMessage.Text += ", S: " + state.StateId;
            if (resId >= 0) lbStatusMessage.Text += ", R: " + resId;
        }

        private void UpdateWindowCaption(string text)
        {
            this.Text = "msstyleEditor - " + text;
        }

        private void UpdateStatusText(string text)
        {
            lbStatusMessage.Text = text;
        }

        private void UpdateImageInfo(Image img)
        {
            if(img == null)
            {
                lbImageInfo.Visible = false;
            }
            else
            {
                lbImageInfo.Text = $"{img.Width}x{img.Height}px";
                lbImageInfo.Visible = true;
            }
        }

        private void OpenStyle(string path)
        {
            try
            {
                m_style = new VisualStyle();
                m_style.Load(path);
                UpdateWindowCaption(path);
            }
            catch(Exception ex)
            {
                MessageBox.Show(this, $"Are you sure this is a Windows Vista or higher visual style?\r\n\r\nDetails: {ex.Message}", "Error loading style!", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
                return;
            }

            m_classView.SetVisualStyle(m_style);

            btFileSave.Enabled = true;
            btFileInfoExport.Enabled = true;
            btImageExport.Enabled = false;
            btImageImport.Enabled = false;
            //btPropertyExport.Enabled = false;
            //btPropertyImport.Enabled = false;
            btPropertyAdd.Enabled = true;
            btPropertyRemove.Enabled = true;
            btTestTheme.Enabled = true;

            if (m_style.StringTable?.Count == 0)
                btFileSave.Style = RibbonButtonStyle.Normal;
            else btFileSave.Style = RibbonButtonStyle.SplitDropDown;

            lbStylePlatform.Text = m_style.Platform.ToDisplayString();

            if (m_style.StringTable.Count == 0)
            {
                MessageBox.Show(this,
                    "Could not locate 'String Table' resource! Some features may not work as expected. " +
                    "Was the file renamed or moved before initially opening it?\r\n\r\n" +
                    "- Make sure the '.msstyles' file is next to its multilingual resource files: '[lang-id]/[name].msstyles.mui'.\r\n" +
                    "- Make sure the names match.\r\n\r\n" +
                    "Then, reopen the file. After properly loading and saving the style, relocating the file is safe."
                    , "Warning, resource is missing!",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return;
            }
        }

        private void CloseStyle()
        {
            m_classView.SetVisualStyle(null);
            m_propertyView.SetStylePart(null, null, null);

            btFileSave.Enabled = false;
            btFileInfoExport.Enabled = false;
            btImageExport.Enabled = false;
            btImageImport.Enabled = false;
            btPropertyExport.Enabled = false;
            btPropertyImport.Enabled = false;
            btPropertyAdd.Enabled = false;
            btPropertyRemove.Enabled = false;
            btTestTheme.Enabled = false;

            lbStylePlatform.Text = "";
            m_style?.Dispose();
        }

        private void DisplayClass(StyleClass cls)
        {
            // remember the shown class
            UpdateItemSelection(cls);
            // reset propery view
            m_propertyView.SetStylePart(m_style, cls, null);
            // reset image view
            m_selectedImage = UpdateImageView(null);
            // reset image selector
{'fraction_non_alphanumeric': 0.08371147985099898, 'fraction_numerical': 0.0025059261767693872, 'mean_word_length': 1.7918321043675554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2259418', 'n_tokens_mistral': 3882, 'n_tokens_neox': 3730, 'n_words': 786}