OmniPhotos
Dialog.cpp
1 #include "Dialog.hpp"
2 #include "Utils/Logger.hpp"
3 
4 //key to everything dialog-related: https://sourceforge.net/projects/tinyfiledialogs/
5 #include <tinyfiledialogs/tinyfiledialogs.h>
6 
7 #include <stdio.h>
8 #include <string.h>
9 
10 #ifdef _MSC_VER
11  #pragma warning(disable : 4996) /* silences warning about strcpy strcat fopen*/
12 #endif
13 
14 
15 Dialog::Dialog()
16 {
17  //tinyfd_verbose = argc - 1;
18  tinyfd_silent = 1;
19 
20 #ifdef _WIN32
21  tinyfd_winUtf8 = 0; /* on windows, you decide if char holds 0(default): MBCS or 1: UTF-8 */
22 #endif
23 
24  lWillBeGraphicMode = tinyfd_inputBox("tinyfd_query", nullptr, nullptr);
25 }
26 
27 
28 void Dialog::init()
29 {
30  strcpy(lBuffer, "v");
31  strcat(lBuffer, tinyfd_version);
32  //ask whether you want graphic mode
33  //if (lWillBeGraphicMode)
34  //{
35  strcat(lBuffer, "\ngraphic mode: "); //go for graphic mode on default
36  //}
37  //else
38  //{
39  // strcat(lBuffer, "\nconsole mode: ");
40  //}
41  strcat(lBuffer, tinyfd_response);
42  strcat(lBuffer, "\n");
43  strcat(lBuffer, tinyfd_needs + 78);
44  strcpy(lString, "hello");
45  tinyfd_messageBox(lString, lBuffer, "ok", "info", 0);
46 
47  tinyfd_notifyPopup("the title", "the message\n\tfrom outer-space", "info");
48 }
49 
50 
51 InputDialog::InputDialog(std::string _description)
52 {
53  if (_description.empty())
54  {
55  LOG(INFO) << "No description passed!\n Better don't enter anything? ;)";
56  }
57  else
58  description = _description;
59  title = "Give me some input";
60 }
61 
62 
63 std::string InputDialog::run()
64 {
65  userInput = std::string(tinyfd_inputBox(
66  title.c_str(), description.c_str(), nullptr));
67  return userInput;
68 }
69 
70 
71 ErrorDialog::ErrorDialog(std::string _message)
72 {
73  if (_message.empty())
74  {
75  LOG(INFO) << "No message passed!";
76  }
77  else
78  message = _message;
79  title = "Error!";
80 }
81 
82 
83 std::string ErrorDialog::run()
84 {
85  tinyfd_messageBox(
86  title.c_str(),
87  message.c_str(),
88  "ok",
89  "error",
90  1);
91  return "error";
92 }
93 
94 
95 QuestionDialog::QuestionDialog(std::string _question) :
96  Dialog()
97 {
98  if (_question.empty())
99  {
100  LOG(INFO) << "No question passed!\n Dialog is useless.";
101  }
102  else
103  question = _question;
104  title = "Question:";
105 }
106 
107 
108 bool QuestionDialog::getBResult()
109 {
110  if (result.compare("Yes") == 0)
111  bResult = true;
112  else
113  {
114  if (result.compare("No") == 0)
115  bResult = false;
116  else
117  {
118  LOG(INFO) << "Invalid answer.";
119  }
120  }
121  return bResult;
122 }
123 
124 
125 std::string QuestionDialog::run()
126 {
127  /*tinyfd_forceConsole = 1;*/
128  if (lWillBeGraphicMode && !tinyfd_forceConsole)
129  {
130  lIntValue = tinyfd_messageBox(title.c_str(),
131  question.c_str(),
132  "yesno", "question", 1);
133  tinyfd_forceConsole = !lIntValue;
134 
135  switch (lIntValue)
136  {
137  case 0: result = "No"; break;
138  case 1: result = "Yes"; break;
139  }
140  /*lIntValue = tinyfd_messageBox("Hello World",
141  "graphic dialogs [yes] / console mode [no]?",
142  "yesnocancel", "question", 1);
143  tinyfd_forceConsole = (lIntValue == 2);*/
144  }
145  return result;
146 }
147 
148 
149 FilesystemDialog::FilesystemDialog(std::string _startFolder)
150 {
151  if (_startFolder.empty())
152  {
153  LOG(INFO) << "No start folder passed!\nSetting to c:/.";
154  startFolder = "C:/";
155  }
156  else
157  {
158  startFolder = _startFolder;
159  }
160 }
161 
162 
163 //FilesystemDialogs
164 ChooseFolderDialog::ChooseFolderDialog(std::string _startFolder) :
165  FilesystemDialog(_startFolder)
166 {
167  title = "Choose folder..";
168 }
169 
170 
171 std::string ChooseFolderDialog::run()
172 {
173  lTheSelectFolderName = tinyfd_selectFolderDialog(
174  "let us just select a directory", nullptr);
175  result = std::string(lTheSelectFolderName);
176  return result;
177 }
178 
179 
180 SaveFileDialog::SaveFileDialog(std::string _startFolder, std::string _data) :
181  FilesystemDialog(_startFolder)
182 {
183  data = _data;
184  title = "Save file";
185 }
186 
187 
188 std::string SaveFileDialog::run()
189 {
190  lTheSaveFileName = tinyfd_saveFileDialog(
191  title.c_str(),
192  startFolder.c_str(),
193  4,
194  lFilterPatterns,
195  nullptr);
196 
197  result = std::string(lTheSaveFileName);
198 
199  //write actual content
200  lIn = fopen(lTheSaveFileName, "w");
201  if (!lIn)
202  {
203  tinyfd_messageBox(
204  "Error",
205  "Can not open this file in write mode",
206  "ok",
207  "error",
208  1);
209  return "Error";
210  }
211  fputs(data.c_str(), lIn);
212  fclose(lIn);
213 
214  return result;
215 }
216 
217 
218 LoadFileDialog::LoadFileDialog(std::string _startFolder) :
219  FilesystemDialog(_startFolder)
220 {
221  title = "Load file";
222 }
223 
224 
225 std::string LoadFileDialog::run()
226 {
227  lTheOpenFileName = tinyfd_openFileDialog(
228  title.c_str(),
229  "",
230  4,
231  lFilterPatterns,
232  nullptr,
233  0);
234 
235  result = std::string(lTheOpenFileName);
236  //load actual content
237  lIn = fopen(lTheOpenFileName, "r");
238  if (!lIn)
239  {
240  tinyfd_messageBox(
241  "Error",
242  "Can not open this file in write mode",
243  "ok",
244  "error",
245  1);
246  return "Error";
247  }
248  lBuffer[0] = '\0';
249  fgets(lBuffer, sizeof(lBuffer), lIn);
250  fclose(lIn);
251 
252  data = lBuffer;
253 
254  return result;
255 }
Definition: Dialog.hpp:6