Skip to content

Commit

Permalink
Encapsulate OpenFileDialog/SaveFileDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Nov 4, 2022
1 parent 2db5235 commit f1f0f16
Show file tree
Hide file tree
Showing 30 changed files with 548 additions and 587 deletions.
123 changes: 123 additions & 0 deletions src/BizHawk.Client.Common/DialogControllerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using System;
using System.Collections.Generic;

namespace BizHawk.Client.Common
{
Expand Down Expand Up @@ -57,6 +58,85 @@ public static bool ModalMessageBox2(
EMsgBoxIcon? icon = null)
=> dialogParent.DialogController.ShowMessageBox3(owner: dialogParent, text: text, caption: caption, icon: icon);

/// <summary>Creates and shows a <c>System.Windows.Forms.OpenFileDialog</c> or equivalent with the receiver (<paramref name="dialogParent"/>) as its parent</summary>
/// <param name="discardCWDChange"><c>OpenFileDialog.RestoreDirectory</c> (isn't this useless when specifying <paramref name="initDir"/>? keeping it for backcompat)</param>
/// <param name="filter"><c>OpenFileDialog.Filter</c></param>
/// <param name="initDir"><c>OpenFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="initFileName"><c>OpenFileDialog.FileName</c>; pre-selected file (overrides <paramref name="initDir"/>?)</param>
/// <returns>filename of selected file, or <see langword="null"/> iff cancelled</returns>
public static string? ShowFileOpenDialog(
this IDialogParent dialogParent,
string initDir,
bool discardCWDChange = false,
FilesystemFilterSet? filter = null,
string? initFileName = null)
=> dialogParent.ShowFileMultiOpenDialog(
discardCWDChange: discardCWDChange,
filterStr: filter?.ToString(),
initDir: initDir,
initFileName: initFileName)?[0];

/// <summary>Creates and shows a <c>System.Windows.Forms.OpenFileDialog</c> or equivalent with the receiver (<paramref name="dialogParent"/>) as its parent</summary>
/// <param name="filter"><c>OpenFileDialog.Filter</c></param>
/// <param name="filterIndex"><c>OpenFileDialog.FilterIndex</c>; initially selected entry in <paramref name="filter"/></param>
/// <param name="initDir"><c>OpenFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="windowTitle"><c>OpenFileDialog.Title</c></param>
/// <returns>filename of selected file, or <see langword="null"/> iff cancelled</returns>
/// <remarks>only used from MainForm, but don't move it there</remarks>
public static string? ShowFileOpenDialog(
this IDialogParent dialogParent,
FilesystemFilterSet? filter,
ref int filterIndex,
string initDir,
string? windowTitle = null)
=> dialogParent.DialogController.ShowFileMultiOpenDialog(
dialogParent: dialogParent,
filterStr: filter?.ToString(),
filterIndex: ref filterIndex,
initDir: initDir,
windowTitle: windowTitle)?[0];

/// <summary>Creates and shows a <c>System.Windows.Forms.OpenFileDialog</c> or equivalent with the receiver (<paramref name="dialogParent"/>) as its parent</summary>
/// <param name="filterStr"><c>OpenFileDialog.Filter</c></param>
/// <param name="initDir"><c>OpenFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="initFileName"><c>OpenFileDialog.FileName</c>; pre-selected file (overrides <paramref name="initDir"/>?)</param>
/// <returns>filename of selected file, or <see langword="null"/> iff cancelled</returns>
/// <remarks>only used from Lua, but don't move it there</remarks>
public static string? ShowFileOpenDialog(
this IDialogParent dialogParent,
string? filterStr,
string initDir,
string? initFileName = null)
=> dialogParent.ShowFileMultiOpenDialog(
filterStr: filterStr,
initDir: initDir,
initFileName: initFileName)?[0];

/// <summary>Creates and shows a <c>System.Windows.Forms.SaveFileDialog</c> or equivalent with the receiver (<paramref name="dialogParent"/>) as its parent</summary>
/// <param name="discardCWDChange"><c>SaveFileDialog.RestoreDirectory</c> (renamed for clarity without inverting value; isn't this useless when specifying <paramref name="initDir"/>? keeping it for backcompat)</param>
/// <param name="fileExt"><c>SaveFileDialog.DefaultExt</c>; used only when the user's chosen filename doesn't have an extension (omit leading '.')</param>
/// <param name="filter"><c>SaveFileDialog.Filter</c></param>
/// <param name="initDir"><c>SaveFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="initFileName"><c>SaveFileDialog.FileName</c>; pre-selected file (overrides <paramref name="initDir"/>?)</param>
/// <param name="muteOverwriteWarning"><c>SaveFileDialog.OverwritePrompt</c> (renamed for clarity with inverted value)</param>
/// <returns>filename of selected destination, or <see langword="null"/> iff cancelled</returns>
public static string? ShowFileSaveDialog(
this IDialogParent dialogParent,
string initDir,
bool discardCWDChange = false,
string? fileExt = null,
FilesystemFilterSet? filter = null,
string? initFileName = null,
bool muteOverwriteWarning = false)
=> dialogParent.DialogController.ShowFileSaveDialog(
dialogParent: dialogParent,
discardCWDChange: discardCWDChange,
fileExt: fileExt,
filterStr: filter?.ToString(),
initDir: initDir,
initFileName: initFileName,
muteOverwriteWarning: muteOverwriteWarning);

/// <summary>
/// Creates and shows a <c>System.Windows.Forms.MessageBox</c> or equivalent without a parent, with the given <paramref name="text"/>,
/// and with the given <paramref name="caption"/> and <paramref name="icon"/> if they're specified.
Expand Down Expand Up @@ -92,5 +172,48 @@ public static bool ShowMessageBox2(
string? caption = null,
EMsgBoxIcon? icon = null)
=> dialogController.ShowMessageBox3(owner: null, text: text, caption: caption, icon: icon);

/// <summary>Creates and shows a <c>System.Windows.Forms.OpenFileDialog</c> or equivalent with the receiver (<paramref name="dialogParent"/>) as its parent</summary>
/// <param name="discardCWDChange"><c>OpenFileDialog.RestoreDirectory</c> (renamed for clarity without inverting value; isn't this useless when specifying <paramref name="initDir"/>? keeping it for backcompat)</param>
/// <param name="filterStr"><c>OpenFileDialog.Filter</c> (call <c>ToString</c> on a <see cref="FilesystemFilter"/>/<see cref="FilesystemFilterSet"/>)</param>
/// <param name="initDir"><c>OpenFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="initFileName"><c>OpenFileDialog.FileName</c>; pre-selected file (overrides <paramref name="initDir"/>?)</param>
/// <param name="maySelectMultiple"><c>OpenFileDialog.Multiselect</c></param>
/// <returns>filenames of selected files, or <see langword="null"/> iff cancelled</returns>
private static IReadOnlyList<string>? ShowFileMultiOpenDialog(
this IDialogParent dialogParent,
string? filterStr,
string initDir,
bool discardCWDChange = false,
string? initFileName = null,
bool maySelectMultiple = false)
{
var filterIndex = 1; // you'd think the default would be 0, but it's not
return dialogParent.DialogController.ShowFileMultiOpenDialog(
dialogParent: dialogParent,
discardCWDChange: discardCWDChange,
filterStr: filterStr,
filterIndex: ref filterIndex,
initDir: initDir,
initFileName: initFileName,
maySelectMultiple: maySelectMultiple);
}

/// <summary>Creates and shows a <c>System.Windows.Forms.OpenFileDialog</c> or equivalent with the receiver (<paramref name="dialogParent"/>) as its parent</summary>
/// <param name="discardCWDChange"><c>OpenFileDialog.RestoreDirectory</c> (isn't this useless when specifying <paramref name="initDir"/>? keeping it for backcompat)</param>
/// <param name="filter"><c>OpenFileDialog.Filter</c></param>
/// <param name="initDir"><c>OpenFileDialog.InitialDirectory</c>; initial browse location</param>
/// <returns>filenames of selected files, or <see langword="null"/> iff cancelled</returns>
public static IReadOnlyList<string>? ShowFileMultiOpenDialog(
this IDialogParent dialogParent,
string initDir,
bool discardCWDChange = false,
FilesystemFilterSet? filter = null)
=> dialogParent.ShowFileMultiOpenDialog(
discardCWDChange: discardCWDChange,
filterStr: filter?.ToString(),
initDir: initDir,
initFileName: null,
maySelectMultiple: true);
}
}
40 changes: 40 additions & 0 deletions src/BizHawk.Client.Common/IDialogController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
#nullable enable

using System.Collections.Generic;

namespace BizHawk.Client.Common
{
public interface IDialogController
{
void AddOnScreenMessage(string message);

/// <summary>Creates and shows a <c>System.Windows.Forms.OpenFileDialog</c> or equivalent</summary>
/// <param name="dialogParent">parent window</param>
/// <param name="discardCWDChange"><c>OpenFileDialog.RestoreDirectory</c> (renamed for clarity without inverting value; isn't this useless when specifying <paramref name="initDir"/>? keeping it for backcompat)</param>
/// <param name="filterStr"><c>OpenFileDialog.Filter</c> (call <c>ToString</c> on a <see cref="FilesystemFilter"/>/<see cref="FilesystemFilterSet"/>)</param>
/// <param name="filterIndex"><c>OpenFileDialog.FilterIndex</c>; initially selected entry in <paramref name="filterStr"/></param>
/// <param name="initDir"><c>OpenFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="initFileName"><c>OpenFileDialog.FileName</c>; pre-selected file (overrides <paramref name="initDir"/>?)</param>
/// <param name="maySelectMultiple"><c>OpenFileDialog.Multiselect</c></param>
/// <param name="windowTitle"><c>OpenFileDialog.Title</c></param>
/// <returns>filenames of selected files, or <see langword="null"/> iff cancelled</returns>
IReadOnlyList<string>? ShowFileMultiOpenDialog(
IDialogParent dialogParent,
string? filterStr,
ref int filterIndex,
string initDir,
bool discardCWDChange = false,
string? initFileName = null,
bool maySelectMultiple = false,
string? windowTitle = null);

/// <summary>Creates and shows a <c>System.Windows.Forms.SaveFileDialog</c> or equivalent</summary>
/// <param name="dialogParent">parent window</param>
/// <param name="discardCWDChange"><c>SaveFileDialog.RestoreDirectory</c> (renamed for clarity without inverting value; isn't this useless when specifying <paramref name="initDir"/>? keeping it for backcompat)</param>
/// <param name="fileExt"><c>SaveFileDialog.DefaultExt</c>; used only when the user's chosen filename doesn't have an extension (omit leading '.')</param>
/// <param name="filterStr"><c>SaveFileDialog.Filter</c> (call <c>ToString</c> on a <see cref="FilesystemFilter"/>/<see cref="FilesystemFilterSet"/>)</param>
/// <param name="initDir"><c>SaveFileDialog.InitialDirectory</c>; initial browse location</param>
/// <param name="initFileName"><c>SaveFileDialog.FileName</c>; pre-selected file (overrides <paramref name="initDir"/>?)</param>
/// <param name="muteOverwriteWarning"><c>SaveFileDialog.OverwritePrompt</c> (renamed for clarity with inverted value)</param>
/// <returns>filename of selected destination, or <see langword="null"/> iff cancelled</returns>
string? ShowFileSaveDialog(
IDialogParent dialogParent,
bool discardCWDChange,
string? fileExt,
string? filterStr,
string initDir,
string? initFileName,
bool muteOverwriteWarning);

/// <summary>
/// Creates and shows a <c>System.Windows.Forms.MessageBox</c> or equivalent with the given <paramref name="text"/>,
/// and with the given <paramref name="owner"/>, <paramref name="caption"/>, and <paramref name="icon"/> if they're specified.
Expand Down
32 changes: 11 additions & 21 deletions src/BizHawk.Client.EmuHawk/AVOut/SynclessRecordingTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@ public SynclessRecordingTools(Config config, IGameInfo game, IDialogController d
#if AVI_SUPPORT
public void Run()
{
var ofd = new OpenFileDialog
{
FileName = $"{_game.FilesystemSafeName()}.syncless.txt",
InitialDirectory = _config.PathEntries.AvAbsolutePath()
};

if (ofd.ShowDialog() == DialogResult.Cancel)
{
return;
}
var result = this.ShowFileOpenDialog(
initDir: _config.PathEntries.AvAbsolutePath(),
initFileName: $"{_game.FilesystemSafeName()}.syncless.txt");
if (result is null) return;

_mSynclessConfigFile = ofd.FileName;
_mSynclessConfigFile = result;

//---- this is pretty crappy:
var lines = File.ReadAllLines(_mSynclessConfigFile);
Expand Down Expand Up @@ -120,23 +114,19 @@ private void BtnExport_Click(object sender, EventArgs e)
height = bmp.Height;
}

var sfd = new SaveFileDialog
{
FileName = Path.ChangeExtension(_mSynclessConfigFile, ".avi")
};
sfd.InitialDirectory = Path.GetDirectoryName(sfd.FileName);
if (sfd.ShowDialog() == DialogResult.Cancel)
{
return;
}
var initFileName = Path.ChangeExtension(_mSynclessConfigFile, ".avi");
var result = this.ShowFileSaveDialog(
initDir: Path.GetDirectoryName(initFileName)!,
initFileName: initFileName);
if (result is null) return;

using var avw = new AviWriter(this);
avw.SetAudioParameters(44100, 2, 16); // hacky
avw.SetMovieParameters(60, 1); // hacky
avw.SetVideoParameters(width, height);
var token = avw.AcquireVideoCodecToken(_config);
avw.SetVideoCodecToken(token);
avw.OpenFile(sfd.FileName);
avw.OpenFile(result);
foreach (var fi in _mFrameInfos)
{
using (var bb = new BitmapBuffer(fi.PngPath, new BitmapLoadOptions()))
Expand Down
18 changes: 7 additions & 11 deletions src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,13 @@ public static void ToClipBoard(this Bitmap bitmap)

public static void SaveAsFile(this Bitmap bitmap, IGameInfo game, string suffix, string systemId, PathEntryCollection paths, IDialogParent parent)
{
using var sfd = new SaveFileDialog
{
FileName = $"{game.FilesystemSafeName()}-{suffix}",
InitialDirectory = paths.ScreenshotAbsolutePathFor(systemId),
Filter = FilesystemFilterSet.Screenshots.ToString(),
RestoreDirectory = true
};

if (parent.ShowDialogWithTempMute(sfd) != DialogResult.OK) return;

var file = new FileInfo(sfd.FileName);
var result = parent.ShowFileSaveDialog(
discardCWDChange: true,
filter: FilesystemFilterSet.Screenshots,
initDir: paths.ScreenshotAbsolutePathFor(systemId),
initFileName: $"{game.FilesystemSafeName()}-{suffix}");
if (result is null) return;
FileInfo file = new(result);
string extension = file.Extension.ToUpper();
ImageFormat i = extension switch
{
Expand Down
Loading

0 comments on commit f1f0f16

Please sign in to comment.