diff --git a/src/nostalgia/studio/CMakeLists.txt b/src/nostalgia/studio/CMakeLists.txt index 7992a5b4..5be9b0e9 100644 --- a/src/nostalgia/studio/CMakeLists.txt +++ b/src/nostalgia/studio/CMakeLists.txt @@ -12,8 +12,8 @@ add_executable( target_link_libraries( nostalgia-studio OxClArgs - NostalgiaCore-Studio NostalgiaStudio + NostalgiaCore-Studio ) if (CMAKE_BUILD_TYPE STREQUAL "Release") diff --git a/src/nostalgia/studio/lib/CMakeLists.txt b/src/nostalgia/studio/lib/CMakeLists.txt index d5b623fd..c474c477 100644 --- a/src/nostalgia/studio/lib/CMakeLists.txt +++ b/src/nostalgia/studio/lib/CMakeLists.txt @@ -1,12 +1,3 @@ -if(APPLE) - enable_language(OBJCXX) -endif() - -if(NOT APPLE AND NOT WIN32) - find_package(PkgConfig REQUIRED) - pkg_check_modules(GTK3 REQUIRED gtk+-3.0) -endif() - add_library( NostalgiaStudio configio.cpp @@ -16,8 +7,7 @@ add_library( task.cpp undostack.cpp widget.cpp - filedialog_gtk.cpp - $<$:filedialog_mac.mm> + filedialog_nfd.cpp ) if(NOT MSVC) @@ -42,7 +32,7 @@ include_directories( target_link_libraries( NostalgiaStudio PUBLIC - ${GTK3_LIBRARIES} + nfd OxEvent NostalgiaCore ) diff --git a/src/nostalgia/studio/lib/filedialog.hpp b/src/nostalgia/studio/lib/filedialog.hpp index f3a85669..863bab22 100644 --- a/src/nostalgia/studio/lib/filedialog.hpp +++ b/src/nostalgia/studio/lib/filedialog.hpp @@ -6,6 +6,13 @@ namespace nostalgia::studio { +struct FDFilterItem { + ox::String name; + ox::String spec; +}; + +ox::Result saveFile(const ox::Vector &exts) noexcept; + ox::Result chooseDirectory() noexcept; } \ No newline at end of file diff --git a/src/nostalgia/studio/lib/filedialog_gtk.cpp b/src/nostalgia/studio/lib/filedialog_gtk.cpp deleted file mode 100644 index 42e46e6a..00000000 --- a/src/nostalgia/studio/lib/filedialog_gtk.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. - */ - -#include - -#if !defined(OX_OS_Windows) && !defined(OX_OS_Darwin) -#include - -#include -#include - -namespace nostalgia::studio { - -ox::Result chooseDirectory() noexcept { - ox::Result out; - const auto action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER; - gtk_init(nullptr, nullptr); - auto dialog = gtk_file_chooser_dialog_new("Open Project Directory", nullptr, action, - ("_Cancel"), GTK_RESPONSE_CANCEL, - ("_Open"), GTK_RESPONSE_ACCEPT, - nullptr); - const auto res = gtk_dialog_run(GTK_DIALOG(dialog)); - if (res == GTK_RESPONSE_ACCEPT) { - auto chooser = GTK_FILE_CHOOSER(dialog); - auto path = gtk_file_chooser_get_filename(chooser); - out = ox::String(path); - g_free(path); - } else { - out = OxError(1); - } - gtk_widget_destroy(dialog); - // for some reason we need to 4 iterations through the main loop to destroy the dialog - for (int i = 0; i < 4; ++i) { - gtk_main_iteration_do(false); - } - return out; -} - -} -#endif \ No newline at end of file diff --git a/src/nostalgia/studio/lib/filedialog_mac.mm b/src/nostalgia/studio/lib/filedialog_mac.mm deleted file mode 100644 index 50695d3f..00000000 --- a/src/nostalgia/studio/lib/filedialog_mac.mm +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016 - 2022 gary@drinkingtea.net - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#import - -#include - -#include "filedialog.hpp" - -namespace nostalgia::studio { - -ox::Result chooseDirectory() noexcept { - auto panel = [NSOpenPanel openPanel]; - [panel setAllowsMultipleSelection:NO]; - [panel setCanChooseDirectories:YES]; - [panel setCanChooseFiles:NO]; - [panel setFloatingPanel:YES]; - if ([panel runModal] == NSModalResponseOK) { - const auto url = [panel URL]; - const auto urlStr = [url absoluteString]; - const auto urlCStr = [urlStr UTF8String]; - return ox::String(urlCStr).substr(7); - } - return OxError(1); -} - -} diff --git a/src/nostalgia/studio/lib/filedialog_nfd.cpp b/src/nostalgia/studio/lib/filedialog_nfd.cpp new file mode 100644 index 00000000..5edbef90 --- /dev/null +++ b/src/nostalgia/studio/lib/filedialog_nfd.cpp @@ -0,0 +1,43 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#include + +#include +#include +#include + +#include "filedialog.hpp" + +namespace nostalgia::studio { + +static ox::Result toResult(nfdresult_t r, const NFD::UniquePathN &path) noexcept { + switch (r) { + case NFD_OKAY: + return ox::String(path.get()); + case NFD_CANCEL: + return OxError(1, "Operation cancelled"); + default: + return OxError(2, NFD::GetError()); + } +} + +ox::Result saveFile(const ox::Vector &filters) noexcept { + NFD::Guard guard; + NFD::UniquePathN path; + ox::Vector filterItems(filters.size()); + for (auto i = 0u; const auto &f : filters) { + filterItems[i] = {f.name.c_str(), f.spec.c_str()}; + ++i; + } + return toResult(NFD::SaveDialog(path, filterItems.data(), filterItems.size()), path); +} + +ox::Result chooseDirectory() noexcept { + NFD::Guard guard; + NFD::UniquePathN path; + return toResult(NFD::PickFolder(path), path); +} + +} \ No newline at end of file