/*
* FileUtils.cs - Utilities for file handling.
*
* Copyright (C) 2004 - 2011 Andreas Scherrer
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
using System.Windows.Forms;
namespace Level9
{
///
/// Utilities for file handling.
///
public class FileUtils
{
///
/// A file dialog filter for Level 9 data files.
///
const string FILTER_DATA_FILES = "All Files (*.*)|*.*"
+ "|Level 9 Data Files (*.dat)|*.dat";
///
/// A file dialog filter for Level 9 game files.
///
const string FILTER_GAME_FILES = "All Files (*.*)|*.*"
+ "|Level 9 Game Files (*.dat; *.sna)|*.dat; *.sna";
///
/// A dialog filter for saved game files.
///
const string FILTER_SAVED_GAME_FILES =
"Saved Game Files (*.sav)|*.sav|All Files (*.*)|*.*";
///
/// A file dialog filter for text files.
///
const string FILTER_TXT_FILES = "Text Files (*.txt)|*.txt"
+ "|All Files (*.*)|*.*";
///
/// A file dialog filter for PNG files.
///
const string FILTER_PNG_FILES = "PNG Files (*.png)|*.png"
+ "|All Files (*.*)|*.*";
///
/// Displays a Open File dialog for Level 9 game files.
///
/// the dialog title
/// the selected filename, or null
public static string OFDGameFiles(string title)
{
return GetOpenFile(title, FILTER_GAME_FILES);
}
///
/// Displays a Open File dialog for saved game files.
///
/// the dialog title
/// the selected filename, or null
public static string OFDSavedGameFiles(string title)
{
return GetOpenFile(title, FILTER_SAVED_GAME_FILES);
}
///
/// Displays a Open File dialog for saved game files.
///
/// the dialog title
/// the selected filename, or null
public static string OFDTextFiles(string title)
{
return GetOpenFile(title, FILTER_TXT_FILES);
}
///
/// Displays a Open File dialog for Level 9 datafiles.
///
/// the dialog title
/// the selected filename, or null
public static string OFDDataFiles(string title)
{
return GetOpenFile(title, FILTER_DATA_FILES);
}
///
/// Displays a Save File dialog for saved game files.
///
/// the dialog title
/// the selected filename, or null
public static string SFDSavedGameFiles(string title)
{
return GetSaveFile(title, FILTER_SAVED_GAME_FILES);
}
///
/// Displays a Save File dialog for Level 9 datafiles.
///
/// the dialog title
/// the selected filename, or null
public static string SFDDataFiles(string title)
{
return GetSaveFile(title, FILTER_DATA_FILES);
}
///
/// Displays a Save File dialog for text files.
///
/// the dialog title
/// the selected filename, or null
public static string SFDTextFiles(string title)
{
return GetSaveFile(title, FILTER_TXT_FILES);
}
///
/// Displays a Save File dialog for PNG files.
///
/// the dialog title
/// the selected filename, or null
public static string SFDPngFiles(string title)
{
return GetSaveFile(title, FILTER_PNG_FILES);
}
///
/// Displays a Open File dialog with the given title and filter.
///
/// the dialog title
/// a file dialog filter
/// the selected filename, or null
static string GetOpenFile(string title, string filter)
{
string filename = null;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = title;
ofd.Filter = filter;
ofd.CheckFileExists = true;
DialogResult result = ofd.ShowDialog();
if (result == DialogResult.OK) {
filename = ofd.FileName;
}
return filename;
}
///
/// Displays a Save File dialog with the given title and filter.
///
/// the dialog title
/// a file dialog filter
/// the selected filename, or null
static string GetSaveFile(string title, string filter)
{
string filename = null;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = title;
sfd.Filter = filter;
sfd.OverwritePrompt = true;
DialogResult result = sfd.ShowDialog();
if (result == DialogResult.OK) {
filename = sfd.FileName;
}
return filename;
}
}
}