kick
/Users/morten/Programmering/cpp/kick/src/kick/context/nativedialog.cpp
00001 //
00002 // Created by Morten Nobel-Jørgensen on 18/04/15.
00003 //
00004 
00005 #include "nativedialog.h"
00006 #include "tinyfiledialogs.h"
00007 #include <sstream>
00008 #include <iostream>
00009 
00010 // http://stackoverflow.com/a/236803/420250
00011 namespace {
00012     std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
00013         std::stringstream ss(s);
00014         std::string item;
00015         while (std::getline(ss, item, delim)) {
00016             elems.push_back(item);
00017         }
00018         return elems;
00019     }
00020     
00021     
00022     std::vector<std::string> split(const std::string &s, char delim) {
00023         std::vector<std::string> elems;
00024         split(s, delim, elems);
00025         return elems;
00026     }
00027 }
00028 
00029 bool kick::NativeDialog::message(std::string aTitle , /* "" */
00030         std::string aMessage , /* "" may contain \n and \t */
00031         DialogType dialogType  ,
00032         IconType infoIcon ,
00033         int const aDefaultButton ) {
00034     char const* aDialogType;
00035     switch (dialogType){
00036         case DialogType::ok:
00037             aDialogType = "ok";
00038             break;
00039         case DialogType::okcancel:
00040             aDialogType = "okcancel";
00041             break;
00042         default:
00043             aDialogType = "yesno";
00044             break;
00045     }
00046     char const * aIconType;
00047     switch (infoIcon){
00048         case IconType::info:
00049             aIconType = "info";
00050             break;
00051         case IconType::error:
00052             aIconType = "error";
00053             break;
00054         case IconType::question:
00055             aIconType = "question";
00056             break;
00057         default:
00058             //case IconType::warning:
00059             aIconType = "warning";
00060             break;
00061     }
00062     return tinyfd_messageBox(aTitle.c_str() ,
00063             aMessage.c_str() ,
00064             aDialogType ,
00065             aIconType ,
00066             aDefaultButton );
00067 }
00068 
00069 std::unique_ptr<std::string> kick::NativeDialog::input(std::string title, std::string message, std::string defaultInput) {
00070     assert(message.find('\n')==std::string::npos);
00071     assert(message.find('\t')==std::string::npos);
00072     char const * res = tinyfd_inputBox (title.c_str(), message.c_str(), defaultInput.c_str());
00073     return std::unique_ptr<std::string>(res?new std::string(res): nullptr);
00074 }
00075 
00076 std::unique_ptr<std::string> kick::NativeDialog::inputPassword(std::string title, std::string message) {
00077     assert(message.find('\n')==std::string::npos);
00078     assert(message.find('\t')==std::string::npos);
00079     char const * res = tinyfd_inputBox (title.c_str(), message.c_str(), nullptr);
00080     return std::unique_ptr<std::string>(res?new std::string(res): nullptr);
00081 }
00082 
00083 std::unique_ptr<std::string> kick::NativeDialog::saveFile(std::string title, std::string aDefaultPathAndFile, std::vector<std::string> filters) {
00084     std::vector<char const *> filtersConverted;
00085     for (auto & f : filters){
00086         filtersConverted.push_back(f.c_str());
00087     }
00088     char const * res = tinyfd_saveFileDialog(title.c_str(), aDefaultPathAndFile.c_str(), filters.size(), filtersConverted.data());
00089     return std::unique_ptr<std::string>(res?new std::string(res): nullptr);
00090 }
00091 
00092 std::vector<std::string> kick::NativeDialog::openFile(std::string  title, std::string aDefaultPathAndFile, std::vector<std::string> filters, bool allowMultipleSelects) {
00093     std::vector<char const *> filtersConverted;
00094     for (auto & f : filters){
00095         filtersConverted.push_back(f.c_str());
00096     }
00097     char const * resStr = tinyfd_openFileDialog(title.c_str(), aDefaultPathAndFile.c_str(), filters.size(), filtersConverted.data(), allowMultipleSelects);
00098     if (resStr){
00099         return split(resStr, '|');
00100     }
00101     return std::vector<std::string>();
00102 }
00103 
00104 std::unique_ptr<std::string> kick::NativeDialog::selectFolder(std::string title, std::string aDefaultPathAndFile) {
00105     char const * res = tinyfd_selectFolderDialog(title.c_str(), aDefaultPathAndFile.c_str());
00106     return std::unique_ptr<std::string>(res?new std::string(res): nullptr);
00107 }
00108 
00109 /*std::unique_ptr<glm::vec3> kick::NativeDialog::colorChooser(std::string title, glm::vec3 defaultRGB) {
00110     unsigned char aDefaultRGB[3];
00111     char hex[7];
00112     hex[6] = '\0';
00113     for (int i=0;i<3;i++){
00114         unsigned char c =(unsigned char)round(defaultRGB[i]*255);
00115         hex[i*2] = '0'+ (c/16);
00116         hex[i*2+1] = '0'+ (c%16);
00117     }
00118     std::cout << "input "<<hex << std::endl;
00119     char const * res = tinyfd_colorChooser(title.c_str(), hex, aDefaultRGB, aDefaultRGB);
00120     if (res){
00121         glm::vec3* c = new glm::vec3();
00122         std::cout << "res "<<res<<std::endl;
00123         for (int i=0;i<3;i++){
00124             (*c)[i] = aDefaultRGB[i]/255.0f;
00125             std::cout<<(*c)[i]<< " "<<(int)aDefaultRGB[i]<<std::endl;
00126         }
00127         return std::unique_ptr<glm::vec3>(c);
00128     }
00129     return std::unique_ptr<glm::vec3>();
00130 }   */
 All Classes Functions Variables