Browse Source

Better fault toleranse when exitin dialogs unusually

merge-requests/365/head
Jørgen Lien Sellæg 10 years ago
parent
commit
893ebe79bf
  1. 6
      src/CMakeLists.txt
  2. 2
      src/dialogs.cc
  3. 77
      src/dialogs_win.cc
  4. 6
      src/singletons.h
  5. 14
      src/window.cc

6
src/CMakeLists.txt

@ -88,7 +88,6 @@ set(source_files juci.h
tooltips.cc
singletons.h
singletons.cc
dialogs.h
cmake.h
cmake.cc)
@ -98,6 +97,7 @@ if(MSYS)
message("MSYS detected")
else()
list(APPEND source_files terminal.cc)
list(APPEND source_files dialogs.h)
list(APPEND source_files dialogs.cc)
message("UNIX detected")
endif()
@ -115,6 +115,7 @@ if(${validation})
${GTKMM_INCLUDE_DIRS}
${GTKSVMM_INCLUDE_DIRS}
${LCL_INCLUDE_DIRS}
"C:/msys64/mingw64/include/dialogs"
${LIBCLANG_INCLUDE_DIRS}
${ASPELL_INCLUDE_DIR})
@ -124,6 +125,7 @@ if(${validation})
${Boost_LIBRARY_DIRS}
# ${PYTHON_INCLUDE_DIRS}
${LCL_LIBRARY_DIRS}
C:/msys64/mingw64/bin
${LIBCLANG_LIBRARY_DIRS})
# set_target_properties(${module}
@ -132,11 +134,11 @@ if(${validation})
# target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
# target_link_libraries(${module} ${Boost_LIBRARIES})
target_link_libraries(${project_name}
${LIBCLANG_LIBRARIES}
${LCL_LIBRARIES}
${GTKMM_LIBRARIES}
"C:/msys64/mingw64/bin/libdialogs.dll"
${GTKSVMM_LIBRARIES}
${Boost_LIBRARIES}
${ASPELL_LIBRARIES}

2
src/dialogs.cc

@ -1,4 +1,4 @@
#include "dialogs.h"
#include <dialogs.h>
#include "singletons.h"
#include <gtkmm.h>
#include <vector>

77
src/dialogs_win.cc

@ -1,87 +1,22 @@
#include "dialogs.h"
#include <windows.h>
#include <shobjidl.h>
#include <memory>
#include <exception>
#include "singletons.h"
#include <sstream>
#ifndef check
#define MESSAGE "An error occurred when trying open windows dialog"
HRESULT __hr__;
#define check(__fun__) __hr__ = __fun__; if(FAILED(__hr__)) Singleton::terminal()->print(MESSAGE)
#endif
class win_string {
public:
win_string() : str(nullptr) { }
~win_string() { CoTaskMemFree(static_cast<void*>(str)); }
std::string operator()(){
std::string res;
if (str != nullptr) {
std::wstringstream ss;
ss << str;
res = std::string(ss.str().begin(), ss.str().end());
}
return res;
}
wchar_t** operator&() { return &str; }
private:
wchar_t* str;
};
class CommonDialog {
public:
CommonDialog() : dialog(nullptr) {
check(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog)));
check(dialog->GetOptions(&options));
}
CommonDialog(const std::string &title, unsigned option) : CommonDialog() {
set_title(title);
add_option(option);
}
void set_title(const std::string &title) { check(dialog->SetTitle(title)); }
void add_option(unsigned option) { check(dialog->SetOptions(options | option)); }
std::string show() {
check(dialog->Show(nullptr));
IShellItem *result = nullptr;
check(dialog->GetResult(&result));
win_string str;
check(result->GetDisplayName(SIGDN_FILESYSPATH, &str));;
result->Release();
return str();
}
private:
IFileOpenDialog * dialog;
DWORD options;
};
enum FILEOPENOPTIONS {
OVERWRITEPROMPT = 0x2, STRICTFILETYPES = 0x4, NOCHANGEDIR = 0x8, PICKFOLDERS = 0x20,
FORCEFILESYSTEM = 0x40, ALLNONSTORAGEITEMS = 0x80, NOVALIDATE = 0x100, ALLOWMULTISELECT = 0x200,
PATHMUSTEXIST = 0x800, FILEMUSTEXIST = 0x1000, CREATEPROMPT = 0x2000, SHAREAWARE = 0x4000,
NOREADONLYRETURN = 0x8000, NOTESTFILECREATE = 0x10000, HIDEMRUPLACES = 0x20000,
HIDEPINNEDPLACES = 0x40000, NODEREFERENCELINKS = 0x100000, DONTADDTORECENT = 0x2000000,
FORCESHOWHIDDEN = 0x10000000, DEFAULTNOMINIMODE = 0x20000000, FORCEPREVIEWPANEON = 0x40000000
};
#include <c_dialogs.h>
std::string Dialog::select_folder() {
return (CommonDialog("Please select a folder", PICKFOLDERS)).show();
return c_select_folder();
}
std::string Dialog::new_file() {
return (CommonDialog("Please select a folder", PICKFOLDERS)).show();
return c_new_file();
}
std::string Dialog::new_folder() {
return (CommonDialog("Please select a folder", PICKFOLDERS)).show();
return c_new_folder();
}
std::string Dialog::select_file() {
return (CommonDialog("Please select a folder", PICKFOLDERS)).show();
return c_select_file();
}
std::string Dialog::save_file() {
return (CommonDialog("Please select a folder", PICKFOLDERS)).show();
return c_save_file();
}

6
src/singletons.h

@ -26,9 +26,9 @@ public:
static std::unique_ptr<Directories::Config> directories_;
static std::unique_ptr<Terminal::Config> terminal_;
};
static std::string config_dir() { return std::string(getenv("HOME")) + "/.juci/config/"; }
static std::string log_dir() { return std::string(getenv("HOME")) + "/.juci/log/"; }
static std::string style_dir() { return std::string(getenv("HOME")) + "/.juci/styles/"; }
static std::string config_dir() { return std::string(getenv("AppData")) + "/.juci/config/"; }
static std::string log_dir() { return std::string(getenv("AppData")) + "/.juci/log/"; }
static std::string style_dir() { return std::string(getenv("AppData")) + "/.juci/styles/"; }
static Terminal *terminal();
static Directories *directories();
static Gtk::Label *status();

14
src/window.cc

@ -38,6 +38,10 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL), notebook(*Singleton::director
set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK);
add(box);
auto i = Gtk::IconTheme::get_default();
for (auto &c : i->get_search_path())
Singleton::terminal()->print(c + "\n");
generate_keybindings();
//PluginApi(&this->notebook, &this->menu);
create_menu();
@ -161,6 +165,7 @@ void Window::create_menu() {
menu.action_group->add(Gtk::Action::create("FileNewFolder", "New Folder"), Gtk::AccelKey(menu.key_map["new_folder"]), [this]() {
auto time_now=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
boost::filesystem::path path = Dialog::new_folder();
if(boost::filesystem::exists(path)) {
if(boost::filesystem::last_write_time(path)>=time_now) {
if(Singleton::directories()->current_path!="")
Singleton::directories()->update();
@ -168,6 +173,9 @@ void Window::create_menu() {
}
else
Singleton::terminal()->print("Error: "+path.string()+" already exists.\n");
} else {
Singleton::terminal()->print("Cancel \n");
}
Singleton::directories()->select(path);
});
menu.action_group->add(Gtk::Action::create("FileNewProject", "New Project"));
@ -204,7 +212,11 @@ void Window::create_menu() {
notebook.open(Dialog::select_file());
});
menu.action_group->add(Gtk::Action::create("FileOpenFolder", "Open Folder"), Gtk::AccelKey(menu.key_map["open_folder"]), [this]() {
Singleton::directories()->open(Dialog::select_folder());
auto path = Dialog::select_folder();
if (boost::filesystem::exists(path))
Singleton::directories()->open(path);
else
Singleton::terminal()->print("Cancel \n");
});
menu.action_group->add(Gtk::Action::create("FileSaveAs", "Save As"), Gtk::AccelKey(menu.key_map["save_as"]), [this]() {
auto path = Dialog::save_file();

Loading…
Cancel
Save