Permalink
Browse files

Added GosuEx::FileDialogs.

Apparently only for Windows XP, Vista and 7, FileDialogs supports "Open File.." and "Save File.." dialogs, including filters, predefined filenames and for Vista/7 the new file dialogs.
  • Loading branch information...
1 parent aad5b1d commit ad0bf199011231d8464babb4a82d522490ad42c7 Quit committed Jul 17, 2010
View
@@ -0,0 +1,12 @@
+#ifdef _MSC_VER
+#ifndef GOSUEX_FILEDIALOGS_AUTOLINK_HPP
+#define GOSUEX_FILEDIALOGS_AUTOLINK_HPP
+
+#ifdef NDEBUG
+#pragma comment(lib, "FileDialogs.lib")
+#else
+#pragma comment(lib, "FileDialogsDebug.lib")
+#endif
+
+#endif
+#endif
@@ -0,0 +1,7 @@
+#include "FileDialogs.hpp"
+
+using namespace GosuEx;
+
+FileDialogs::Filter::Filter(const std::wstring& name, const std::wstring& pattern) { this->name = name; this->pattern = pattern; }
+
+FileDialogs::FilterList& FileDialogs::FilterList::operator%(Filter& filter) { filters.push_back(filter); return *this; }
@@ -0,0 +1,50 @@
+#ifndef GOSUEX_FILEDIALOGS_FILEDIALOGS_HPP
+#define GOSUEX_FILEDIALOGS_FILEDIALOGS_HPP
+
+#include <GosuEx/FileDialogs/Fwd.hpp>
+#include <Gosu/Fwd.hpp>
+
+#include <string>
+#include <vector>
+
+namespace GosuEx {
+ namespace FileDialogs {
+
+ // Filters are used to specify allowed file types.
+ struct Filter {
+ std::wstring name, pattern;
+ Filter(const std::wstring& name, const std::wstring& pattern);
+ };
+
+
+ // A filterlist is a list of filters that can be passed to open/save dialogs.
+ // You can use it like that: FilterList() % Filter(L"All Files", L"*.*") % Filter(L"Images", L"*.png;*.jpg;*.jpeg").
+ struct FilterList {
+ std::vector<Filter> filters;
+ FilterList& operator%(Filter& filter);
+ };
+
+ // This is the "parent" of all dialogs.
+ class FileDialog {
+ public:
+ virtual void setFilename(const std::wstring& name) = 0;
+ };
+
+ // Parent of all "Open File..." dialogs.
+ class OpenFileDialog : public FileDialog {
+ public:
+ virtual std::wstring open() = 0;
+ };
+
+ // Parent of all "Save File..." dialogs.
+ class SaveFileDialog : public FileDialog {
+ public:
+ virtual std::wstring save() = 0;
+ };
+
+ // Just call these.
+ OpenFileDialog* getOpenFileDialog(Gosu::Window& win, const std::wstring& initialdir = L"", const std::wstring& title = L"", FilterList& filters = FilterList());
+ SaveFileDialog* getSaveFileDialog(Gosu::Window& win, const std::wstring& initialdir = L"", const std::wstring& title = L"", FilterList& filters = FilterList());
+ }
+}
+#endif
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup Label="ProjectConfigurations">
+ <ProjectConfiguration Include="Debug|Win32">
+ <Configuration>Debug</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ <ProjectConfiguration Include="Release|Win32">
+ <Configuration>Release</Configuration>
+ <Platform>Win32</Platform>
+ </ProjectConfiguration>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="AutoLink.hpp" />
+ <ClInclude Include="FileDialogs.hpp" />
+ <ClInclude Include="FileDialogsWin.hpp" />
+ <ClInclude Include="Fwd.hpp" />
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="FileDialogs.cpp" />
+ <ClCompile Include="FileDialogsWin.cpp" />
+ </ItemGroup>
+ <PropertyGroup Label="Globals">
+ <ProjectGuid>{D7FF49B7-DBCC-4248-8759-D42A1CC67F1A}</ProjectGuid>
+ <Keyword>Win32Proj</Keyword>
+ <RootNamespace>FileDialogs</RootNamespace>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+ <ConfigurationType>StaticLibrary</ConfigurationType>
+ <UseDebugLibraries>true</UseDebugLibraries>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+ <ConfigurationType>StaticLibrary</ConfigurationType>
+ <UseDebugLibraries>false</UseDebugLibraries>
+ <WholeProgramOptimization>true</WholeProgramOptimization>
+ <CharacterSet>Unicode</CharacterSet>
+ </PropertyGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+ <ImportGroup Label="ExtensionSettings">
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+ </ImportGroup>
+ <PropertyGroup Label="UserMacros" />
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <OutDir>$(ProjectDir)..\lib\</OutDir>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <OutDir>$(ProjectDir)..\lib\</OutDir>
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <TargetName>$(ProjectName)Debug</TargetName>
+ </PropertyGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+ <ClCompile>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <WarningLevel>Level3</WarningLevel>
+ <Optimization>Disabled</Optimization>
+ <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Windows</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ </Link>
+ </ItemDefinitionGroup>
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+ <ClCompile>
+ <WarningLevel>Level3</WarningLevel>
+ <PrecompiledHeader>
+ </PrecompiledHeader>
+ <Optimization>MaxSpeed</Optimization>
+ <FunctionLevelLinking>true</FunctionLevelLinking>
+ <IntrinsicFunctions>true</IntrinsicFunctions>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
+ </ClCompile>
+ <Link>
+ <SubSystem>Windows</SubSystem>
+ <GenerateDebugInformation>true</GenerateDebugInformation>
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>
+ <OptimizeReferences>true</OptimizeReferences>
+ </Link>
+ </ItemDefinitionGroup>
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+ <ImportGroup Label="ExtensionTargets">
+ </ImportGroup>
+</Project>
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Source Files">
+ <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+ <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+ </Filter>
+ <Filter Include="Header Files">
+ <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+ <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
+ </Filter>
+ <Filter Include="Resource Files">
+ <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+ <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="Fwd.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="FileDialogsWin.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="FileDialogs.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="AutoLink.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ </ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="FileDialogsWin.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="FileDialogs.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ </ItemGroup>
+</Project>
@@ -0,0 +1,56 @@
+#include "FileDialogsWin.hpp"
+
+using namespace GosuEx;
+
+// Statics for W7
+template<typename T> HMODULE GosuEx::FileDialogs::FileDialog7<T>::ShellDLL = NULL;
+template<typename T> HRESULT(WINAPI* GosuEx::FileDialogs::FileDialog7<T>::SHCIFPN)(PCWSTR, IBindCtx*, REFIID, void**);
+
+// XP
+FileDialogs::OpenFileDialogXP::OpenFileDialogXP(Gosu::Window& win, const std::wstring& initialdir, const std::wstring& title, FilterList& filters): FileDialogXP<OpenFileDialog>(win, initialdir, title, filters) {}
+
+std::wstring FileDialogs::OpenFileDialogXP::open() {
+ if (GetOpenFileName(&this->p))
+ return this->filename;
+ return L"";
+}
+
+FileDialogs::SaveFileDialogXP::SaveFileDialogXP(Gosu::Window& win, const std::wstring& initialdir, const std::wstring& title, FilterList& filters): FileDialogXP<SaveFileDialog>(win, initialdir, title, filters) {}
+
+std::wstring FileDialogs::SaveFileDialogXP::save() {
+ if (GetSaveFileName(&this->p))
+ return this->filename;
+ return L"";
+}
+
+// Win7
+FileDialogs::OpenFileDialog7::OpenFileDialog7(Gosu::Window& win, const std::wstring& initialdir, const std::wstring& title, FilterList& filters): FileDialog7<OpenFileDialog>(CLSID_FileOpenDialog, win, initialdir, title, filters) {}
+
+std::wstring FileDialogs::OpenFileDialog7::open() {
+ return _result();
+}
+
+FileDialogs::SaveFileDialog7::SaveFileDialog7(Gosu::Window& win, const std::wstring& initialdir, const std::wstring& title, FilterList& filters): FileDialog7<SaveFileDialog>(CLSID_FileSaveDialog, win, initialdir, title, filters) {}
+
+std::wstring FileDialogs::SaveFileDialog7::save() {
+ return _result();
+}
+
+// Determinate whether or not to use the new dialogs
+bool FileDialogs::useNewDialogs() {
+ return GetProcAddress(LoadLibrary(L"Shell32.dll"), "SHCreateItemFromParsingName") != NULL;
+}
+
+// And the functions to get the dialogs
+FileDialogs::OpenFileDialog* FileDialogs::getOpenFileDialog(Gosu::Window& win, const std::wstring& initialdir, const std::wstring& title, FilterList& filters) {
+ if (useNewDialogs())
+ return new OpenFileDialog7(win, initialdir, title, filters);
+ return new OpenFileDialogXP(win, initialdir, title, filters);
+}
+
+FileDialogs::SaveFileDialog* FileDialogs::getSaveFileDialog(Gosu::Window& win, const std::wstring& initialdir, const std::wstring& title, FilterList& filters) {
+ if (useNewDialogs())
+ return new SaveFileDialog7(win, initialdir, title, filters);
+ else
+ return new SaveFileDialogXP(win, initialdir, title, filters);
+}
Oops, something went wrong.

0 comments on commit ad0bf19

Please sign in to comment.