UiFile.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* UiFile.cpp
  2. *
  3. * Copyright (C) 1992-2018 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "UiP.h"
  19. #include "Editor.h"
  20. static void UiFile_init (UiForm me, GuiWindow parent, conststring32 title) {
  21. my d_dialogParent = parent;
  22. Thing_setName (me, title);
  23. }
  24. MelderFile UiFile_getFile (UiForm me) {
  25. return & my file;
  26. }
  27. /********** READING A FILE **********/
  28. autoUiForm UiInfile_create (GuiWindow parent, conststring32 title,
  29. UiCallback okCallback, void *okClosure,
  30. conststring32 invokingButtonTitle, conststring32 helpTitle, bool allowMultipleFiles)
  31. {
  32. autoUiForm me = Thing_new (UiForm);
  33. my okCallback = okCallback;
  34. my buttonClosure = okClosure;
  35. my invokingButtonTitle = Melder_dup (invokingButtonTitle);
  36. my helpTitle = Melder_dup (helpTitle);
  37. my allowMultipleFiles = allowMultipleFiles;
  38. UiFile_init (me.get(), parent, title);
  39. return me;
  40. }
  41. void UiInfile_do (UiForm me) {
  42. try {
  43. autoStringSet infileNames = GuiFileSelect_getInfileNames (my d_dialogParent, my name.get(), my allowMultipleFiles);
  44. for (integer ifile = 1; ifile <= infileNames->size; ifile ++) {
  45. SimpleString infileName = infileNames->at [ifile];
  46. Melder_pathToFile (infileName -> string.get(), & my file);
  47. UiHistory_write (U"\n");
  48. UiHistory_write_colonize (my invokingButtonTitle.get());
  49. UiHistory_write (U" \"");
  50. UiHistory_write_expandQuotes (infileName -> string.get());
  51. UiHistory_write (U"\"");
  52. structMelderFile file { };
  53. MelderFile_copy (& my file, & file);
  54. try {
  55. my okCallback (me, 0, nullptr, nullptr, nullptr, my invokingButtonTitle.get(), false, my buttonClosure);
  56. } catch (MelderError) {
  57. Melder_throw (U"File ", & file, U" not finished.");
  58. }
  59. }
  60. } catch (MelderError) {
  61. Melder_flushError ();
  62. }
  63. }
  64. /********** WRITING A FILE **********/
  65. autoUiForm UiOutfile_create (GuiWindow parent, conststring32 title,
  66. UiCallback okCallback, void *okClosure, conststring32 invokingButtonTitle, conststring32 helpTitle)
  67. {
  68. autoUiForm me = Thing_new (UiForm);
  69. my okCallback = okCallback;
  70. my buttonClosure = okClosure;
  71. my invokingButtonTitle = Melder_dup (invokingButtonTitle);
  72. my helpTitle = Melder_dup (helpTitle);
  73. UiFile_init (me.get(), parent, title);
  74. my allowExecutionHook = theAllowExecutionHookHint;
  75. my allowExecutionClosure = theAllowExecutionClosureHint;
  76. return me;
  77. }
  78. static void commonOutfileCallback (UiForm sendingForm, integer narg, Stackel args, conststring32 sendingString,
  79. Interpreter interpreter, conststring32 /* invokingButtonTitle */, bool /* modified */, void *closure)
  80. {
  81. EditorCommand command = (EditorCommand) closure;
  82. command -> commandCallback (command -> d_editor, command, sendingForm, narg, args, sendingString, interpreter);
  83. }
  84. autoUiForm UiOutfile_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle) {
  85. Editor editor = cmd -> d_editor;
  86. autoUiForm dia = UiOutfile_create (editor -> windowForm, title, commonOutfileCallback, cmd, invokingButtonTitle, helpTitle);
  87. dia -> command = cmd;
  88. return dia;
  89. }
  90. autoUiForm UiInfile_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle) {
  91. Editor editor = cmd -> d_editor;
  92. autoUiForm dia = UiInfile_create (editor -> windowForm, title, commonOutfileCallback, cmd, invokingButtonTitle, helpTitle, false);
  93. dia -> command = cmd;
  94. return dia;
  95. }
  96. void UiOutfile_do (UiForm me, conststring32 defaultName) {
  97. autostring32 outfileName = GuiFileSelect_getOutfileName (nullptr, my name.get(), defaultName);
  98. if (! outfileName) return; // cancelled
  99. if (my allowExecutionHook && ! my allowExecutionHook (my allowExecutionClosure)) {
  100. Melder_flushError (U"Dialog \"", my name.get(), U"\" cancelled.");
  101. return;
  102. }
  103. Melder_pathToFile (outfileName.get(), & my file);
  104. structMelderFile file { };
  105. MelderFile_copy (& my file, & file); // save, because okCallback could destroy me
  106. UiHistory_write (U"\n");
  107. UiHistory_write_colonize (my invokingButtonTitle.get());
  108. try {
  109. my okCallback (me, 0, nullptr, nullptr, nullptr, my invokingButtonTitle.get(), false, my buttonClosure);
  110. } catch (MelderError) {
  111. Melder_flushError (U"File ", & file, U" not finished.");
  112. }
  113. UiHistory_write (U" \"");
  114. UiHistory_write (outfileName.get());
  115. UiHistory_write (U"\"");
  116. }
  117. /* End of file UiFile.cpp */