Browse Source

Cleanup of dialogs, as well as fixing a couple of crashes.

merge-requests/365/head
U-ole-PC\ole 10 years ago
parent
commit
ec4fc631ba
  1. 10
      src/CMakeLists.txt
  2. 6
      src/dialogs.cc
  3. 6
      src/dialogs.h
  4. 126
      src/dialogs_win.cc
  5. 6
      src/directories.cc
  6. 6
      src/window.cc

10
src/CMakeLists.txt

@ -28,15 +28,7 @@ find_package(LibClang REQUIRED)
#find_package(PythonLibs 2.7) #find_package(PythonLibs 2.7)
#find_package(Boost 1.55 COMPONENTS python thread log system filesystem REQUIRED) #find_package(Boost 1.55 COMPONENTS python thread log system filesystem REQUIRED)
find_package(Boost 1.55 COMPONENTS thread log system filesystem REQUIRED)
set(BOOST_DEP thread log system filesystem)
if(MSYS)
list(APPEND BOOST_DEP locale)
endif()
find_package(Boost 1.55 COMPONENTS ${BOOST_DEP} REQUIRED)
pkg_check_modules(GTKMM gtkmm-3.0 REQUIRED) # The name GTKMM is set here for the variables abouve pkg_check_modules(GTKMM gtkmm-3.0 REQUIRED) # The name GTKMM is set here for the variables abouve

6
src/dialogs.cc

@ -25,7 +25,7 @@ std::string open_dialog(const std::string &title,
return dialog.run() == Gtk::RESPONSE_OK ? dialog.get_filename() : ""; return dialog.run() == Gtk::RESPONSE_OK ? dialog.get_filename() : "";
} }
std::string Dialog::select_folder() { std::string Dialog::open_folder() {
return open_dialog("Please choose a folder", return open_dialog("Please choose a folder",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Open", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Open", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
@ -43,13 +43,13 @@ std::string Dialog::new_folder() {
Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER);
} }
std::string Dialog::select_file() { std::string Dialog::open_file() {
return open_dialog("Please choose a file", return open_dialog("Please choose a file",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_OPEN); Gtk::FILE_CHOOSER_ACTION_OPEN);
} }
std::string Dialog::save_file(const boost::filesystem::path file_path) { std::string Dialog::save_file_as(const boost::filesystem::path &file_path) {
return open_dialog("Please choose a file", return open_dialog("Please choose a file",
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Save", Gtk::RESPONSE_OK)}, {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Save", Gtk::RESPONSE_OK)},
Gtk::FILE_CHOOSER_ACTION_SAVE, file_path.string()); Gtk::FILE_CHOOSER_ACTION_SAVE, file_path.string());

6
src/dialogs.h

@ -5,11 +5,11 @@
class Dialog { class Dialog {
public: public:
static std::string select_folder(); static std::string open_folder();
static std::string select_file(); static std::string open_file();
static std::string new_file(); static std::string new_file();
static std::string new_folder(); static std::string new_folder();
static std::string save_file(const boost::filesystem::path file_path); static std::string save_file_as(const boost::filesystem::path &file_path);
}; };
#endif //JUCI_DIALOG_H_ #endif //JUCI_DIALOG_H_

126
src/dialogs_win.cc

@ -1,6 +1,5 @@
#include "dialogs.h" #include "dialogs.h"
#include "singletons.h" #include "singletons.h"
#include <boost/locale.hpp>
#undef NTDDI_VERSION #undef NTDDI_VERSION
#define NTDDI_VERSION NTDDI_VISTA #define NTDDI_VERSION NTDDI_VISTA
@ -14,26 +13,16 @@
#include <iostream> //TODO: remove #include <iostream> //TODO: remove
using namespace std; //TODO: remove using namespace std; //TODO: remove
#ifndef check
HRESULT __hr__;
#define check(__fun__, error_message) \
__hr__ = __fun__; \
if (FAILED(__hr__)) { \
Singleton::terminal->print(error_message); \
throw std::runtime_error(error_message); \
}
#endif // CHECK
class CommonDialog { class CommonDialog {
public: public:
CommonDialog(CLSID type); CommonDialog(CLSID type);
/** available options are listed https://msdn.microsoft.com/en-gb/library/windows/desktop/dn457282(v=vs.85).aspx */ /** available options are listed at https://msdn.microsoft.com/en-gb/library/windows/desktop/dn457282(v=vs.85).aspx */
void add_option(unsigned option); void add_option(unsigned option);
void set_title(const std::string &title); void set_title(const std::wstring &title);
/** Sets the extensions the browser can find */ /** Sets the extensions the browser can find */
void set_default_file_extension(const std::string &file_extension); void set_default_file_extension(const std::wstring &file_extension);
/** Sets the directory to start browsing */ /** Sets the directory to start browsing */
void set_default_folder(const std::string &directory_path); void set_default_folder(const std::wstring &directory_path);
/** Returns the selected item's path as a string */ /** Returns the selected item's path as a string */
std::string show(); std::string show();
@ -45,55 +34,64 @@ protected:
class OpenDialog : public CommonDialog { class OpenDialog : public CommonDialog {
public: public:
OpenDialog(const std::string &title, unsigned option); OpenDialog(const std::wstring &title, unsigned option=0);
}; };
class SaveDialog : public CommonDialog { class SaveDialog : public CommonDialog {
public: public:
SaveDialog(const std::string &title, unsigned option); SaveDialog(const std::wstring &title, const boost::filesystem::path &file_path="", unsigned option=0);
private: private:
std::vector<COMDLG_FILTERSPEC> extensions; std::vector<COMDLG_FILTERSPEC> extensions;
}; };
// { COMMON_DIALOG
CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) { CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) {
if(CoCreateInstance(type, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))!=S_OK) { if(CoCreateInstance(type, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))!=S_OK) {
error=true; error=true;
return; return;
} }
if(dialog->GetOptions(&options)!=S_OK) if(dialog->GetOptions(&options)!=S_OK)
error=true; error=true;
} }
void CommonDialog::set_title(const std::string &title) {
void CommonDialog::set_title(const std::wstring &title) {
if(error) return; if(error) return;
auto ptr = boost::locale::conv::utf_to_utf<wchar_t>(title).data(); if(dialog->SetTitle(title.c_str())!=S_OK)
if(dialog->SetTitle(ptr)!=S_OK)
error=true; error=true;
} }
void CommonDialog::add_option(unsigned option) { void CommonDialog::add_option(unsigned option) {
if(error) return; if(error) return;
if(dialog->SetOptions(options | option)!=S_OK) if(dialog->SetOptions(options | option)!=S_OK)
error=true; error=true;
} }
void CommonDialog::set_default_file_extension(const std::string &file_extension) {
void CommonDialog::set_default_file_extension(const std::wstring &file_extension) {
if(error) return; if(error) return;
auto ptr = boost::locale::conv::utf_to_utf<wchar_t>(file_extension).data(); if(dialog->SetDefaultExtension(file_extension.c_str())!=S_OK)
if(dialog->SetDefaultExtension(ptr)!=S_OK)
error=true; error=true;
} }
void CommonDialog::set_default_folder(const std::string &directory_path) {
void CommonDialog::set_default_folder(const std::wstring &directory_path) {
if(error) return; if(error) return;
std::wstring path=directory_path;
size_t pos=0;
while((pos=path.find(L'/', pos))!=std::wstring::npos) {//TODO: issue bug report on boost::filesystem::path::native on MSYS2
path.replace(pos, 1, L"\\");
pos++;
}
IShellItem * folder = nullptr; IShellItem * folder = nullptr;
auto ptr = boost::locale::conv::utf_to_utf<wchar_t>(directory_path).data(); if(SHCreateItemFromParsingName(path.c_str(), nullptr, IID_PPV_ARGS(&folder))!=S_OK) {
if(SHCreateItemFromParsingName(ptr, nullptr, IID_PPV_ARGS(&folder))!=S_OK) {
error=true; error=true;
return; return;
} }
auto result=dialog->SetDefaultFolder(folder) auto hresult=dialog->SetDefaultFolder(folder);
folder->Release(); folder->Release();
if(result!=S_OK) if(hresult!=S_OK)
error=true; error=true;
} }
std::string CommonDialog::show() { std::string CommonDialog::show() {
if(error) return ""; if(error) return "";
if(dialog->Show(nullptr)!=S_OK) { if(dialog->Show(nullptr)!=S_OK) {
@ -107,54 +105,62 @@ std::string CommonDialog::show() {
return ""; return "";
} }
LPWSTR str = nullptr; LPWSTR str = nullptr;
auto result=result->GetDisplayName(SIGDN_FILESYSPATH, &str); auto hresult=result->GetDisplayName(SIGDN_FILESYSPATH, &str);
result->Release(); result->Release();
dialog->Release(); dialog->Release();
if(result!=S_OK) if(hresult!=S_OK)
return ""; return "";
auto res = boost::locale::conv::utf_to_utf<char>(str); std::wstring wstr(str);
std::string res(wstr.begin(), wstr.end());
CoTaskMemFree(str); CoTaskMemFree(str);
return res; return res;
} }
OpenDialog::OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { OpenDialog::OpenDialog(const std::wstring &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) {
set_title(title); set_title(title);
add_option(option); add_option(option);
auto dirs = Singleton::directories()->current_path; auto dirs = Singleton::directories->current_path;
set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); set_default_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native());
} }
SaveDialog::SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { SaveDialog::SaveDialog(const std::wstring &title, const boost::filesystem::path &file_path, unsigned option) : CommonDialog(CLSID_FileSaveDialog) {
set_title(title); set_title(title);
add_option(option); add_option(option);
auto dirs = Singleton::directories()->current_path; if(!file_path.empty()) {
set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); set_default_folder(file_path.parent_path().native());
//extensions.emplace_back(COMDLG_FILTERSPEC{L"Default", L"*.h;*.cpp"}); if(file_path.has_extension()) {
//extensions.emplace_back(COMDLG_FILTERSPEC{L"GoogleStyle", L"*.cc;*.h"}); auto extension=(L"*"+file_path.extension().native()).c_str();
//extensions.emplace_back(COMDLG_FILTERSPEC{L"BoostStyle", L"*.hpp;*.cpp"}); extensions.emplace_back(COMDLG_FILTERSPEC{extension, extension});
//extensions.emplace_back(COMDLG_FILTERSPEC{L"Other", L"*.cxx;*.c"}); set_default_file_extension(extension);
//check(dialog->SetFileTypes(extensions.size(), extensions.data()), "Failed to set extensions"); }
//set_default_file_extension("Default"); }
else {
auto dirs = Singleton::directories->current_path;
set_default_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native());
}
extensions.emplace_back(COMDLG_FILTERSPEC{L"All files", L"*.*"});
if(dialog->SetFileTypes(extensions.size(), extensions.data())!=S_OK) {
error=true;
return;
}
} }
// DIALOGS }} std::string Dialog::open_folder() {
std::string Dialog::select_folder() { return (OpenDialog(L"Open Folder", FOS_PICKFOLDERS)).show();
return (OpenDialog("Select folder", FOS_PICKFOLDERS)).show();
} }
std::string Dialog::new_file() { std::string Dialog::new_file() {
return (SaveDialog("Please choose your destination", 0)).show(); return (SaveDialog(L"New File")).show();
} }
std::string Dialog::new_folder() { std::string Dialog::new_folder() {
return Dialog::select_folder(); return (OpenDialog(L"New Folder", FOS_PICKFOLDERS)).show();
} }
std::string Dialog::select_file() { std::string Dialog::open_file() {
return (OpenDialog("Open file", 0)).show(); return (OpenDialog(L"Open File")).show();
} }
std::string Dialog::save_file(const boost::filesystem::path file_path) { std::string Dialog::save_file_as(const boost::filesystem::path &file_path) {
//TODO: use file_path return (SaveDialog(L"Save File As", file_path)).show();
return (SaveDialog("Please choose your destination", 0)).show();
} }

6
src/directories.cc

@ -118,9 +118,9 @@ Directories::~Directories() {
} }
void Directories::open(const boost::filesystem::path& dir_path) { void Directories::open(const boost::filesystem::path& dir_path) {
JDEBUG("start"); JDEBUG("start");
if(dir_path.empty()) if(dir_path.empty())
return; return;
tree_store->clear(); tree_store->clear();
update_mutex.lock(); update_mutex.lock();

6
src/window.cc

@ -215,12 +215,12 @@ void Window::set_menu_actions() {
}); });
menu->add_action("open_file", [this]() { menu->add_action("open_file", [this]() {
auto path=Dialog::select_file(); auto path=Dialog::open_file();
if(path!="") if(path!="")
notebook.open(path); notebook.open(path);
}); });
menu->add_action("open_folder", [this]() { menu->add_action("open_folder", [this]() {
auto path = Dialog::select_folder(); auto path = Dialog::open_folder();
if (path!="" && boost::filesystem::exists(path)) if (path!="" && boost::filesystem::exists(path))
Singleton::directories->open(path); Singleton::directories->open(path);
}); });
@ -242,7 +242,7 @@ void Window::set_menu_actions() {
}); });
menu->add_action("save_as", [this]() { menu->add_action("save_as", [this]() {
if(notebook.get_current_page()!=-1) { if(notebook.get_current_page()!=-1) {
auto path = Dialog::save_file(notebook.get_current_view()->file_path); auto path = Dialog::save_file_as(notebook.get_current_view()->file_path);
if(path!="") { if(path!="") {
std::ofstream file(path); std::ofstream file(path);
if(file) { if(file) {

Loading…
Cancel
Save