| 1 |
/* UiFile.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2018,2022,2023 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 |
|
| 19 |
#include "UiP.h" |
| 20 |
#include "ScriptEditor.h" |
| 21 |
|
| 22 |
static void UiFile_init (UiForm me, GuiWindow parent, conststring32 title) { |
| 23 |
my d_dialogParent = parent; |
| 24 |
Thing_setName (me, title); |
| 25 |
} |
| 26 |
|
| 27 |
MelderFile UiFile_getFile (UiForm me) { |
| 28 |
return & my file; |
| 29 |
} |
| 30 |
|
| 31 |
/********** READING A FILE **********/ |
| 32 |
|
| 33 |
autoUiForm UiInfile_create (GuiWindow parent, Editor optionalEditor, conststring32 title, |
| 34 |
UiCallback okCallback, void *okClosure, |
| 35 |
conststring32 invokingButtonTitle, conststring32 helpTitle, bool allowMultipleFiles) |
| 36 |
{ |
| 37 |
autoUiForm me = Thing_new (UiForm); |
| 38 |
my optionalEditor = optionalEditor; |
| 39 |
my okCallback = okCallback; |
| 40 |
my buttonClosure = okClosure; |
| 41 |
my invokingButtonTitle = Melder_dup (invokingButtonTitle); |
| 42 |
my helpTitle = Melder_dup (helpTitle); |
| 43 |
my allowMultipleFiles = allowMultipleFiles; |
| 44 |
UiFile_init (me.get(), parent, title); |
| 45 |
return me; |
| 46 |
} |
| 47 |
|
| 48 |
void UiInfile_do (UiForm me) { |
| 49 |
try { |
| 50 |
autoStringSet infileNames = GuiFileSelect_getInfileNames (my d_dialogParent, my name.get(), my allowMultipleFiles); |
| 51 |
for (integer ifile = 1; ifile <= infileNames->size; ifile ++) { |
| 52 |
SimpleString infileName = infileNames->at [ifile]; |
| 53 |
Melder_pathToFile (infileName -> string.get(), & my file); |
| 54 |
UiHistory_write (U"\n"); |
| 55 |
UiHistory_write_colonize (my invokingButtonTitle.get()); |
| 56 |
UiHistory_write (U" \""); |
| 57 |
UiHistory_write_expandQuotes (infileName -> string.get()); |
| 58 |
UiHistory_write (U"\""); |
| 59 |
structMelderFile file { }; |
| 60 |
MelderFile_copy (& my file, & file); |
| 61 |
try { |
| 62 |
my okCallback (me, 0, nullptr, nullptr, nullptr, my invokingButtonTitle.get(), false, my buttonClosure, my optionalEditor); |
| 63 |
} catch (MelderError) { |
| 64 |
Melder_throw (U"File ", & file, U" not finished."); |
| 65 |
} |
| 66 |
} |
| 67 |
} catch (MelderError) { |
| 68 |
Melder_flushError (); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/********** WRITING A FILE **********/ |
| 73 |
|
| 74 |
autoUiForm UiOutfile_create (GuiWindow parent, Editor optionalEditor, conststring32 title, |
| 75 |
UiCallback okCallback, void *okClosure, conststring32 invokingButtonTitle, conststring32 helpTitle) |
| 76 |
{ |
| 77 |
autoUiForm me = Thing_new (UiForm); |
| 78 |
my optionalEditor = optionalEditor; |
| 79 |
my okCallback = okCallback; |
| 80 |
my buttonClosure = okClosure; |
| 81 |
my invokingButtonTitle = Melder_dup (invokingButtonTitle); |
| 82 |
my helpTitle = Melder_dup (helpTitle); |
| 83 |
UiFile_init (me.get(), parent, title); |
| 84 |
my allowExecutionHook = theAllowExecutionHookHint; |
| 85 |
my allowExecutionClosure = theAllowExecutionClosureHint; |
| 86 |
return me; |
| 87 |
} |
| 88 |
|
| 89 |
static void commonOutfileCallback (UiForm sendingForm, integer narg, Stackel args, conststring32 sendingString, |
| 90 |
Interpreter interpreter, conststring32 /* invokingButtonTitle */, bool /* modified */, void *closure, Editor /* optionalEditor */) |
| 91 |
{ |
| 92 |
EditorCommand command = (EditorCommand) closure; |
| 93 |
command -> commandCallback (command -> sender, command, sendingForm, narg, args, sendingString, interpreter); |
| 94 |
} |
| 95 |
|
| 96 |
autoUiForm UiOutfile_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle) { |
| 97 |
Editor editor = cmd -> d_editor; |
| 98 |
autoUiForm dia = UiOutfile_create (editor -> windowForm, editor, title, commonOutfileCallback, cmd, invokingButtonTitle, helpTitle); |
| 99 |
dia -> command = cmd; |
| 100 |
return dia; |
| 101 |
} |
| 102 |
|
| 103 |
autoUiForm UiInfile_createE (EditorCommand cmd, conststring32 title, conststring32 invokingButtonTitle, conststring32 helpTitle) { |
| 104 |
Editor editor = cmd -> d_editor; |
| 105 |
autoUiForm dia = UiInfile_create (editor -> windowForm, editor, title, commonOutfileCallback, cmd, invokingButtonTitle, helpTitle, false); |
| 106 |
dia -> command = cmd; |
| 107 |
return dia; |
| 108 |
} |
| 109 |
|
| 110 |
void UiOutfile_do (UiForm me, conststring32 defaultName) { |
| 111 |
autostring32 outfileName = GuiFileSelect_getOutfileName (my d_dialogParent, my name.get(), defaultName); |
| 112 |
if (! outfileName) |
| 113 |
return; // cancelled |
| 114 |
if (my allowExecutionHook && ! my allowExecutionHook (my allowExecutionClosure)) { |
| 115 |
Melder_flushError (U"Dialog “", my name.get(), U"” cancelled."); |
| 116 |
return; |
| 117 |
} |
| 118 |
Melder_pathToFile (outfileName.get(), & my file); |
| 119 |
structMelderFile file { }; |
| 120 |
MelderFile_copy (& my file, & file); // save, because okCallback could destroy me |
| 121 |
UiHistory_write (U"\n"); |
| 122 |
UiHistory_write_colonize (my invokingButtonTitle.get()); |
| 123 |
try { |
| 124 |
my okCallback (me, 0, nullptr, nullptr, nullptr, my invokingButtonTitle.get(), false, my buttonClosure, my optionalEditor); |
| 125 |
} catch (MelderError) { |
| 126 |
Melder_flushError (U"File ", & file, U" not finished."); |
| 127 |
} |
| 128 |
UiHistory_write (U" \""); |
| 129 |
UiHistory_write (outfileName.get()); |
| 130 |
UiHistory_write (U"\""); |
| 131 |
} |
| 132 |
|
| 133 |
/* End of file UiFile.cpp */ |