From 496f0a5b51ab35c4d3f4efb14675ecd1861c658d Mon Sep 17 00:00:00 2001 From: Nathanne Isip Date: Mon, 28 Oct 2024 12:05:04 +0800 Subject: [PATCH] Added ui.dialog function for file chooser and message boxes. --- lib/zhvlib/UI.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++- lib/zhvlib/UI.hpp | 5 +++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/lib/zhvlib/UI.cc b/lib/zhvlib/UI.cc index 1dd73ff..3034ea8 100644 --- a/lib/zhvlib/UI.cc +++ b/lib/zhvlib/UI.cc @@ -93,6 +93,86 @@ ZHIVO_FUNC(ui_main) { return DynamicObject(); } +ZHIVO_FUNC(ui_dialog_openFile) { + if(args.size() > 1) + throw std::runtime_error( + "Expecting 1 argument, got " + + std::to_string(args.size()) + + "." + ); + + DynamicObject uuid = args.at(0); + if(windowDictionary.find(uuid.toString()) == windowDictionary.end()) + throw std::runtime_error("Cannot find window with specified key"); + + return DynamicObject(std::string( + uiOpenFile(windowDictionary[uuid.toString()]) + )); +} + +ZHIVO_FUNC(ui_dialog_saveFile) { + if(args.size() > 1) + throw std::runtime_error( + "Expecting 1 argument, got " + + std::to_string(args.size()) + + "." + ); + + DynamicObject uuid = args.at(0); + if(windowDictionary.find(uuid.toString()) == windowDictionary.end()) + throw std::runtime_error("Cannot find window with specified key"); + + return DynamicObject(std::string( + uiSaveFile(windowDictionary[uuid.toString()]) + )); +} + +ZHIVO_FUNC(ui_dialog_messageBox) { + if(args.size() != 3) + throw std::runtime_error( + "Expecting 3 argument, got " + + std::to_string(args.size()) + + "." + ); + + DynamicObject uuid = args.at(0), + title = args.at(1), + caption = args.at(2); + + if(windowDictionary.find(uuid.toString()) == windowDictionary.end()) + throw std::runtime_error("Cannot find window with specified key"); + + uiMsgBox( + windowDictionary[uuid.toString()], + title.toString().c_str(), + caption.toString().c_str() + ); + return DynamicObject(); +} + +ZHIVO_FUNC(ui_dialog_messageBoxError) { + if(args.size() != 3) + throw std::runtime_error( + "Expecting 3 argument, got " + + std::to_string(args.size()) + + "." + ); + + DynamicObject uuid = args.at(0), + title = args.at(1), + caption = args.at(2); + + if(windowDictionary.find(uuid.toString()) == windowDictionary.end()) + throw std::runtime_error("Cannot find window with specified key"); + + uiMsgBoxError( + windowDictionary[uuid.toString()], + title.toString().c_str(), + caption.toString().c_str() + ); + return DynamicObject(); +} + ZHIVO_FUNC(ui_window_create) { if(args.size() != 4) throw std::runtime_error( @@ -141,7 +221,7 @@ ZHIVO_FUNC(ui_window_onClosing) { std::string uuidStr = uuid.toString(); if(windowDictionary.find(uuidStr) != windowDictionary.end()) window = windowDictionary[uuidStr]; - else throw std::runtime_error("Cannot window with specified key"); + else throw std::runtime_error("Cannot find window with specified key"); windowCallbackMap[uuidStr] = [callback, uuidStr, symtab]() -> int { std::shared_ptr func = callback.getCallable(); diff --git a/lib/zhvlib/UI.hpp b/lib/zhvlib/UI.hpp index 31a82e3..c20fa06 100644 --- a/lib/zhvlib/UI.hpp +++ b/lib/zhvlib/UI.hpp @@ -27,6 +27,11 @@ ZHIVO_FUNC(ui_init); ZHIVO_FUNC(ui_quit); ZHIVO_FUNC(ui_main); +ZHIVO_FUNC(ui_dialog_openFile); +ZHIVO_FUNC(ui_dialog_saveFile); +ZHIVO_FUNC(ui_dialog_messageBox); +ZHIVO_FUNC(ui_dialog_messageBoxError); + ZHIVO_FUNC(ui_window_create); ZHIVO_FUNC(ui_window_onClosing); ZHIVO_FUNC(ui_window_show);