// Constructor.
        public MainWindow(IntPtr devicePointer)
        {
            InitializeComponent();

            // Register window loaded listener.
            Loaded += Window_Loaded;

            // Register window closed listener.
            Closed += Window_Closed;

            // Load the program settings.
            Settings = SettingsHandler.GetInstance();

            // Start the packet capture.
            Device = devicePointer;
            PacketCaptureThread = new Thread(new ThreadStart(StartPacketCapture));
            PacketCaptureThread.Start();
        }
        // Called when the window has been loaded.
        public void Window_Loaded(object sender, EventArgs args)
        {
            // Register button events
            SaveButton.Click += SaveButton_Click;
            CancelButton.Click += CancelButton_Click;
            DefaultFontButton.Click += DefaultFontButton_Click;

            // Register easter egg (spoiler?)
            FONT_NAME.TextChanged += FontName_TextChanged;

            // Load settings.
            LoadedSettings = SettingsHandler.CloneInstance();

            // Fill settings in input fields.
            EXE_NAME.Text = LoadedSettings.ExeName;
            FONT_NAME.Text = LoadedSettings.FontName;
            MESSAGES_SHOWN.Text = LoadedSettings.MessagesShown.ToString();
            TRANSLATE_TO.Text = LoadedSettings.TranslateTo;
            AUTO_HIDE.IsChecked = LoadedSettings.AutoHide;
            ADD_ON_STARTUP.IsChecked = LoadedSettings.AddOnStartup;
            OUTPUT_ALL.IsChecked = LoadedSettings.OutputAll;
        }
 // Merges an instance with the static instance.
 public void Merge(SettingsHandler settingsHandler)
 {
     this.ExeName = settingsHandler.ExeName;
     this.FontName = settingsHandler.FontName;
     this.TranslateTo = settingsHandler.TranslateTo;
     this.MessagesShown = settingsHandler.MessagesShown;
     this.AutoHide = settingsHandler.AutoHide;
     this.AddOnStartup = settingsHandler.AddOnStartup;
     this.OutputAll = settingsHandler.OutputAll;
 }
        // Returns a copy of the static instance. This copy can not save itself to disk.
        public SettingsHandler Clone()
        {
            SettingsHandler handler = new SettingsHandler();
            handler.CanSave = false;

            handler.ExeName = this.ExeName;
            handler.FontName = this.FontName;
            handler.TranslateTo = this.TranslateTo;
            handler.MessagesShown = this.MessagesShown;
            handler.AutoHide = this.AutoHide;
            handler.AddOnStartup = this.AddOnStartup;
            handler.OutputAll = this.OutputAll;

            return handler;
        }
        // Static access combining SettingsHandler.Merge() and SettingsHandler.SaveSettings().
        public static void MergeAndSave(SettingsHandler handler)
        {
            // Make sure there is an instance available.
            GetInstance();

            // Don't merge what does not need to be merged.
            if (handler != Instance)
                Instance.Merge(handler);

            // Save settings to disk.
            Instance.SaveSettings();
        }
        // Returns the static instance or creates one if none is available.
        public static SettingsHandler GetInstance()
        {
            // No previous instance is available - Create one.
            if (Instance == null)
            {
                Instance = new SettingsHandler();

                // Attempt to load saved settings from a file.
                Instance.LoadSettings();
            }

            return Instance;
        }
        // Returns a copy of the static instance. This copy can not save itself to disk.
        public SettingsHandler Clone()
        {
            SettingsHandler handler = new SettingsHandler();
            handler.CanSave = false;

            handler.ExeName = this.ExeName;
            handler.FontName = this.FontName;
            handler.TranslateTo = this.TranslateTo;
            handler.MessagesShown = this.MessagesShown;
            handler.AutoHide = this.AutoHide;
            handler.AddOnStartup = this.AddOnStartup;
            handler.OutputAll = this.OutputAll;
            handler.FadeMessages = this.FadeMessages;
            handler.FadeWait = this.FadeWait;
            handler.FadeDuration = this.FadeDuration;
            handler.AutoMessageHeight = this.AutoMessageHeight;
            handler.MessageHeight = this.MessageHeight;
            handler.DefaultAdapterMAC = this.DefaultAdapterMAC;
            handler.UseDefaultAdapter = this.UseDefaultAdapter;
            handler.AutoDetectPort = this.AutoDetectPort;
            handler.AutoDetectProgram = this.AutoDetectProgram;

            return handler;
        }
 // Merges an instance with the static instance.
 public void Merge(SettingsHandler settingsHandler)
 {
     this.ExeName = settingsHandler.ExeName;
     this.FontName = settingsHandler.FontName;
     this.TranslateTo = settingsHandler.TranslateTo;
     this.MessagesShown = settingsHandler.MessagesShown;
     this.AutoHide = settingsHandler.AutoHide;
     this.AddOnStartup = settingsHandler.AddOnStartup;
     this.OutputAll = settingsHandler.OutputAll;
     this.FadeMessages = settingsHandler.FadeMessages;
     this.FadeWait = settingsHandler.FadeWait;
     this.FadeDuration = settingsHandler.FadeDuration;
     this.AutoMessageHeight = settingsHandler.AutoMessageHeight;
     this.MessageHeight = settingsHandler.MessageHeight;
     this.DefaultAdapterMAC = settingsHandler.DefaultAdapterMAC;
     this.UseDefaultAdapter = settingsHandler.UseDefaultAdapter;
     this.AutoDetectPort = settingsHandler.AutoDetectPort;
     this.AutoDetectProgram = settingsHandler.AutoDetectProgram;
 }
        // Called when the window has been loaded.
        public void Window_Loaded(object sender, EventArgs args)
        {
            // Register button events
            SaveButton.Click += SaveButton_Click;
            CancelButton.Click += CancelButton_Click;
            DefaultFontButton.Click += DefaultFontButton_Click;

            // Register easter egg (spoiler?)
            FONT_NAME.SelectionChanged += FontName_SelectionChanged;

            // Register event for AutoMessageHeight change.
            AUTO_MESSAGE_HEIGHT.Checked += AUTO_MESSAGE_HEIGHT_Checked;
            AUTO_MESSAGE_HEIGHT.Unchecked += AUTO_MESSAGE_HEIGHT_Checked;
            AUTO_DETECT_PORT.Checked += AUTO_DETECT_PORT_Checked;
            AUTO_DETECT_PORT.Unchecked += AUTO_DETECT_PORT_Checked;

            // Load fonts.
            LoadFonts();

            // Load settings.
            LoadedSettings = SettingsHandler.CloneInstance();

            // Fill settings in input fields.
            EXE_NAME.Text = LoadedSettings.ExeName;
            SavedFont = LoadedSettings.FontName;
            SetFont(SavedFont);
            MESSAGES_SHOWN.Text = LoadedSettings.MessagesShown.ToString();
            TRANSLATE_TO.Text = LoadedSettings.TranslateTo;
            AUTO_HIDE.IsChecked = LoadedSettings.AutoHide;
            ADD_ON_STARTUP.IsChecked = LoadedSettings.AddOnStartup;
            OUTPUT_ALL.IsChecked = LoadedSettings.OutputAll;
            FADE_MESSAGES.IsChecked = LoadedSettings.FadeMessages;
            FADE_WAIT.Text = LoadedSettings.FadeWait.ToString();
            FADE_DURATION.Text = LoadedSettings.FadeDuration.ToString();
            AUTO_MESSAGE_HEIGHT.IsChecked = LoadedSettings.AutoMessageHeight;
            MESSAGE_HEIGHT.Text = LoadedSettings.MessageHeight.ToString();
            AUTO_DETECT_PORT.IsChecked = LoadedSettings.AutoDetectPort;
            AUTO_DETECT_PROGRAM.Text = LoadedSettings.AutoDetectProgram;

            if (LoadedSettings.DefaultAdapterMAC.Length > 0)
            {
                USE_DEFAULT_ADAPTER.IsChecked = LoadedSettings.UseDefaultAdapter;
            }
            else
            {
                USE_DEFAULT_ADAPTER.IsChecked = false;
                USE_DEFAULT_ADAPTER.IsEnabled = false;
            }
        }