context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using GitCommands;
  using GitUI.CommandsDialogs;
  using GitUI.Hotkey;
  using ICSharpCode.TextEditor.Util;
  using PatchApply;
  using GitCommands.Settings;
  using GitUI.CommandsDialogs.SettingsDialog.Pages;
  using GitUI.Editor.Diff;
  using ResourceManager;
  
  namespace GitUI.Editor
  {
      [DefaultEvent("SelectedLineChanged")]
      public partial class FileViewer : GitModuleControl
      {
          private readonly AsyncLoader _async;
          private int _currentScrollPos = -1;
          private bool _currentViewIsPatch;
          private readonly IFileViewer _internalFileViewer;
  
          public FileViewer()
          {
              TreatAllFilesAsText = false;
              ShowEntireFile = false;
              NumberOfVisibleLines = AppSettings.NumberOfContextLines;
              InitializeComponent();
              Translate();
  
              GitUICommandsSourceSet += FileViewer_GitUICommandsSourceSet;
  
              _internalFileViewer = new FileViewerInternal();
              _internalFileViewer.MouseEnter += _internalFileViewer_MouseEnter;
              _internalFileViewer.MouseLeave += _internalFileViewer_MouseLeave;
              _internalFileViewer.MouseMove += _internalFileViewer_MouseMove;
  
              var internalFileViewerControl = (Control)_internalFileViewer;
              internalFileViewerControl.Dock = DockStyle.Fill;
              Controls.Add(internalFileViewerControl);
  
              _async = new AsyncLoader();
              _async.LoadingError +=
                  (sender, args) =>
                  {
                      if (!IsDisposed)
                      {
                          ResetForText(null);
                          _internalFileViewer.SetText("Unsupported file: \n\n" + args.Exception.ToString());
                          if (TextLoaded != null)
                              TextLoaded(this, null);
                      }
                  };
  
              IgnoreWhitespaceChanges = AppSettings.IgnoreWhitespaceChanges;
              ignoreWhiteSpaces.Checked = IgnoreWhitespaceChanges;
              ignoreWhitespaceChangesToolStripMenuItem.Checked = IgnoreWhitespaceChanges;
  
              ShowEntireFile = AppSettings.ShowEntireFile;
              showEntireFileButton.Checked = ShowEntireFile;
              showEntireFileToolStripMenuItem.Checked = ShowEntireFile;
  
              showNonPrintChars.Checked = AppSettings.ShowNonPrintingChars;
              showNonprintableCharactersToolStripMenuItem.Checked = AppSettings.ShowNonPrintingChars;
              ToggleNonPrintingChars(AppSettings.ShowNonPrintingChars);
  
              IsReadOnly = true;
              var encodingList = AppSettings.AvailableEncodings.Values.Select(e => e.EncodingName).ToList();
              var defaultEncodingName = Encoding.Default.EncodingName;
  
              for (int i = 0; i < encodingList.Count; i++)
              {
                  if (string.Compare(encodingList[i], defaultEncodingName, true) == 0)
                  {
                      encodingList[i] = "Default (" + Encoding.Default.HeaderName + ")";
                      break;
                  }
              }
              encodingToolStripComboBox.Items.AddRange(encodingList.ToArray());
  
              _internalFileViewer.MouseMove += TextAreaMouseMove;
              _internalFileViewer.MouseLeave += TextAreaMouseLeave;
              _internalFileViewer.TextChanged += TextEditor_TextChanged;
              _internalFileViewer.ScrollPosChanged += _internalFileViewer_ScrollPosChanged;
              _internalFileViewer.SelectedLineChanged += _internalFileViewer_SelectedLineChanged;
              _internalFileViewer.DoubleClick += (sender, args) => OnRequestDiffView(EventArgs.Empty);
  
              this.HotkeysEnabled = true;
  
              if (RunTime() && ContextMenuStrip == null)
                  ContextMenuStrip = contextMenu;
              contextMenu.Opening += ContextMenu_Opening;
          }
  
          void FileViewer_GitUICommandsSourceSet(object sender, GitUICommandsSourceEventArgs e)
          {
              UICommandsSource.GitUICommandsChanged += WorkingDirChanged;
              WorkingDirChanged(UICommandsSource, null);
          }
  
          protected override void DisposeUICommandsSource()
          {
              UICommandsSource.GitUICommandsChanged -= WorkingDirChanged;
              base.DisposeUICommandsSource();
          }
  
          private bool RunTime()
          {
              return (System.Diagnostics.Process.GetCurrentProcess().ProcessName != "devenv");
          }
  
          [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
          [Browsable(false)]
          public new Font Font
          {
              get { return _internalFileViewer.Font; }
              set { _internalFileViewer.Font = value; }
          }
  
          [Description("Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.")]
          [DefaultValue(false)]
          public bool IgnoreWhitespaceChanges { get; set; }
          [Description("Show diffs with <n> lines of context.")]
          [DefaultValue(3)]
          public int NumberOfVisibleLines { get; set; }
          [Description("Show diffs with entire file.")]
          [DefaultValue(false)]
          public bool ShowEntireFile { get; set; }
          [Description("Treat all files as text.")]
          [DefaultValue(false)]
          public bool TreatAllFilesAsText { get; set; }
          [DefaultValue(true)]
          [Category("Behavior")]
          public bool IsReadOnly
          {
              get { return _internalFileViewer.IsReadOnly; }
              set { _internalFileViewer.IsReadOnly = value; }
          }
          [DefaultValue(true)]
          [Description("If true line numbers are shown in the textarea")]
          [Category("Appearance")]
          public bool ShowLineNumbers
          {
              get { return _internalFileViewer.ShowLineNumbers; }
              set { _internalFileViewer.ShowLineNumbers = value; }
          }
  
          private Encoding _Encoding;
          [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
          [Browsable(false)]
          public Encoding Encoding
          {
              get
              {
                  if (_Encoding == null)
                      _Encoding = Module.FilesEncoding;
  
                  return _Encoding;
              }
  
              set
              {
                  _Encoding = value;
              }
          }
  
          [DefaultValue(0)]
          [Browsable(false)]
          public int ScrollPos
          {
              get { return _internalFileViewer.ScrollPos; }
              set { _internalFileViewer.ScrollPos = value; }
          }
  
          [Browsable(false)]
          public byte[] FilePreabmle { get; private set; }
  
          private void WorkingDirChanged(object sender, GitUICommandsChangedEventArgs e)
          {
              this.Encoding = null;
          }
  
          protected override void OnRuntimeLoad(EventArgs e)
          {
              this.Hotkeys = HotkeySettingsManager.LoadHotkeys(HotkeySettingsName);
              Font = AppSettings.DiffFont;
          }
  
          void ContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
          {
              copyToolStripMenuItem.Enabled = (_internalFileViewer.GetSelectionLength() > 0);
              if (ContextMenuOpening != null)
                  ContextMenuOpening(sender, e);
          }
  
          void _internalFileViewer_MouseMove(object sender, MouseEventArgs e)
          {
              this.OnMouseMove(e);
          }
  
          void _internalFileViewer_MouseEnter(object sender, EventArgs e)
          {
              this.OnMouseEnter(e);
          }
  
          void _internalFileViewer_MouseLeave(object sender, EventArgs e)
          {
              this.OnMouseLeave(e);
          }
  
          void _internalFileViewer_SelectedLineChanged(object sender, SelectedLineEventArgs e)
          {
              if (SelectedLineChanged != null)
                  SelectedLineChanged(sender, e);
          }
  
          public event EventHandler<SelectedLineEventArgs> SelectedLineChanged;
  
          public event EventHandler ScrollPosChanged;
          public event EventHandler RequestDiffView;
          public new event EventHandler TextChanged;
          public event EventHandler TextLoaded;
          public event CancelEventHandler ContextMenuOpening;
  
          public ToolStripSeparator AddContextMenuSeparator()
          {
              ToolStripSeparator separator = new ToolStripSeparator();
              contextMenu.Items.Add(separator);
              return separator;
          }
  
          public ToolStripMenuItem AddContextMenuEntry(string text, EventHandler toolStripItem_Click)
          {
              ToolStripMenuItem toolStripItem = new ToolStripMenuItem(text);
              contextMenu.Items.Add(toolStripItem);
              toolStripItem.Click += toolStripItem_Click;
              return toolStripItem;
          }
  
          protected virtual void OnRequestDiffView(EventArgs args)
          {
              var handler = RequestDiffView;
  
              if (handler != null)
              {
                  handler(this, args);
              }
          }
  
          void _internalFileViewer_ScrollPosChanged(object sender, EventArgs e)
          {
              if (ScrollPosChanged != null)
                  ScrollPosChanged(sender, e);
          }
  
          public void EnableScrollBars(bool enable)
          {
              _internalFileViewer.EnableScrollBars(enable);
          }
  
          void TextEditor_TextChanged(object sender, EventArgs e)
          {
              if (patchHighlighting)
                  _internalFileViewer.AddPatchHighlighting();
  
              if (TextChanged != null)
                  TextChanged(sender, e);
          }
  
          private void UpdateEncodingCombo()
          {
  
gt:             if (Encoding != null)