<commit_before>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using L10NSharp;
using SIL.PlatformUtilities;
using SIL.Reporting;
#if !MONO
using System.Drawing.Imaging;
using SIL.IO;
using WIA;
#endif

namespace SIL.Windows.Forms.ImageToolbox
{
	/// <summary>
	/// Provides 4 ways to get an image: Gallery (art of reading), scanner, camera, or file system.
	/// </summary>
	public partial class AcquireImageControl : UserControl, IImageToolboxControl
	{
		private PalasoImage _previousImage;
		private PalasoImage _currentImage;

		public event EventHandler ImageChanged;

		public AcquireImageControl()
		{
			InitializeComponent();

			if (!Platform.IsWindows)
			{
				_scannerButton.Enabled = _cameraButton.Enabled = false;
				// Mono layout doesn't always handle Anchor point properly.  Fix it.
				FixMyLayoutForMono();
			}

			_galleryControl.ImageChanged += _galleryControl_ImageChanged;
		}

		void _galleryControl_ImageChanged(object sender, EventArgs e)
		{
			//propogate that event to the outer toolbox
			if (ImageChanged != null)
				ImageChanged(sender, e);
		}

		private bool _actModal;

		private void OnGetFromFileSystemClick(object sender, EventArgs e)
		{
			if (_actModal)
				return;		// The GTK file chooser on Linux isn't acting modal, so we simulate it.
			_actModal = true;
			try
			{
				SetMode(Modes.SingleImage);
				// The primary thing that OpenFileDialogWithViews buys us is the ability to default to large icons.
				// OpenFileDialogWithViews still doesn't let us read (and thus remember) the selected view.
				using (var dlg = new OpenFileDialogWithViews(OpenFileDialogWithViews.DialogViewTypes.Large_Icons))
				{
					if (string.IsNullOrEmpty(ImageToolboxSettings.Default.LastImageFolder))
					{
						dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
					}
					else
					{
						dlg.InitialDirectory = ImageToolboxSettings.Default.LastImageFolder; ;
					}

					dlg.Title = "Choose Picture File".Localize("ImageToolbox.FileChooserTitle", "Title shown for a file chooser dialog brought up by the ImageToolbox");

					//NB: dissallowed gif because of a .net crash:  http://jira.palaso.org/issues/browse/BL-85
					dlg.Filter = "picture files".Localize("ImageToolbox.PictureFiles", "Shown in the file-picking dialog to describe what kind of files the dialog is filtering for") + "(*.png;*.tif;*.tiff;*.jpg;*.jpeg;*.bmp)|*.png;*.tif;*.tiff;*.jpg;*.jpeg;*.bmp;";

					if (DialogResult.OK == dlg.ShowDialog(this.ParentForm))
					{<commit_msg>Tweak SWF image tool use of GTK file chooser on Linux<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using L10NSharp;
using SIL.PlatformUtilities;
using SIL.Reporting;
#if !MONO
using System.Drawing.Imaging;
using SIL.IO;
using WIA;
#endif

namespace SIL.Windows.Forms.ImageToolbox
{
	/// <summary>
	/// Provides 4 ways to get an image: Gallery (art of reading), scanner, camera, or file system.
	/// </summary>
	public partial class AcquireImageControl : UserControl, IImageToolboxControl
	{
		private PalasoImage _previousImage;
		private PalasoImage _currentImage;

		public event EventHandler ImageChanged;

		public AcquireImageControl()
		{
			InitializeComponent();

			if (!Platform.IsWindows)
			{
				_scannerButton.Enabled = _cameraButton.Enabled = false;
				// Mono layout doesn't always handle Anchor point properly.  Fix it.
				FixMyLayoutForMono();
			}

			_galleryControl.ImageChanged += _galleryControl_ImageChanged;
		}

		void _galleryControl_ImageChanged(object sender, EventArgs e)
		{
			//propogate that event to the outer toolbox
			if (ImageChanged != null)
				ImageChanged(sender, e);
		}

		private bool _actModal;

		private void OnGetFromFileSystemClick(object sender, EventArgs e)
		{
			if (_actModal)
				return;		// The GTK file chooser on Linux isn't acting modal, so we simulate it.
			_actModal = true;
			// Something in the Mono runtime state machine keeps the GTK filechooser from getting the
			// focus immediately when we invoke OpenFileDialogWithViews.ShowDialog() directly at this
			// point.  Waiting for the next idle gets it into a state where the filechooser does receive
			// the focus as desired.  See https://silbloom.myjetbrains.com/youtrack/issue/BL-5809.
			if (Platform.IsMono)
				Application.Idle += DelayGetImageFileFromSystem;
			else
				GetImageFileFromSystem();
		}

		private void DelayGetImageFileFromSystem(object sender, EventArgs e)
		{
			Application.Idle -= DelayGetImageFileFromSystem;
			GetImageFileFromSystem();
		}

		private void GetImageFileFromSystem()
		{
			try
			{
				SetMode(Modes.SingleImage);
				// The primary thing that OpenFileDialogWithViews buys us is the ability to default to large icons.
				// OpenFileDialogWithViews still doesn't let us read (and thus remember) the selected view.
				using (var dlg = new OpenFileDialogWithViews(OpenFileDialogWithViews.DialogViewTypes.Large_Icons))
				{
					if (string.IsNullOrEmpty(ImageToolboxSettings.Default.LastImageFolder))
					{
						dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
					}
					else
					{
						dlg.InitialDirectory = ImageToolboxSettings.Default.LastImageFolder; ;
					}

					dlg.Title = "Choose Picture File".Localize("ImageToolbox.FileChooserTitle", "Title shown for a file chooser dialog brought up by the ImageToolbox");

					//NB: dissallowed gif because of a .net crash:  http://jira.palaso.org/issues/browse/BL-85
					dlg.Filter = "picture files".Localize("ImageToolbox.PictureFiles", "Shown in the file-picking dialog to describe what kind of files the dialog is filtering for") + "(*.png;*.tif;*.tiff;*.jpg;*.jpeg;*.bmp)|*.png;*.tif;*.tiff;*.jpg;*.jpeg;*.bmp;";

					if (DialogResult.OK == dlg.ShowDialog(this.ParentForm))
					{
{'fraction_non_alphanumeric': 0.09633878859456724, 'fraction_numerical': 0.002362071874472752, 'mean_word_length': 3.9235880398671097, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '5391019', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1769, 'n_words': 503}