findobj mode/xlat/scale/rotate, save backup shortcut, undo limit bug, title boundary...
[goodguy/history.git] / cinelerra-5.1 / cinelerra / savefile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "confirmsave.h"
23 #include "bchash.h"
24 #include "edl.h"
25 #include "errorbox.h"
26 #include "file.h"
27 #include "filexml.h"
28 #include "fileformat.h"
29 #include "indexfile.h"
30 #include "language.h"
31 #include "mainmenu.h"
32 #include "mwindow.h"
33 #include "mwindowgui.h"
34 #include "playback3d.h"
35 #include "savefile.h"
36 #include "mainsession.h"
37
38 #include <string.h>
39
40
41
42
43
44
45
46
47
48 SaveBackup::SaveBackup(MWindow *mwindow)
49  : BC_MenuItem(_("Save backup"), "b", 'b')
50 {
51         this->mwindow = mwindow;
52 }
53 int SaveBackup::handle_event()
54 {
55         mwindow->save_backup();
56         mwindow->gui->show_message(_("Saved backup."));
57         return 1;
58 }
59
60
61
62
63
64
65
66
67
68
69
70 Save::Save(MWindow *mwindow) : BC_MenuItem(_("Save"), "s", 's')
71 {
72         this->mwindow = mwindow;
73         quit_now = 0;
74 }
75
76 void Save::create_objects(SaveAs *saveas)
77 {
78         this->saveas = saveas;
79 }
80
81 int Save::handle_event()
82 {
83         if(mwindow->session->filename[0] == 0)
84         {
85                 saveas->start();
86         }
87         else
88         {
89 // save it
90 // TODO: Move this into mwindow.
91                 FileXML file;
92                 mwindow->edl->save_xml(&file, mwindow->session->filename);
93                 file.terminate_string();
94
95                 if(file.write_to_file(mwindow->session->filename))
96                 {
97                         char string2[256];
98                         sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
99                         ErrorBox error(_(PROGRAM_NAME ": Error"),
100                                 mwindow->gui->get_abs_cursor_x(1),
101                                 mwindow->gui->get_abs_cursor_y(1));
102                         error.create_objects(string2);
103                         error.raise_window();
104                         error.run_window();
105                         return 1;
106                 }
107                 else
108                 {
109                         char string[BCTEXTLEN];
110                         sprintf(string, _("\"%s\" %dC written"),
111                                  mwindow->session->filename, (int)strlen(file.string()));
112                         mwindow->gui->show_message(string);
113                 }
114                 mwindow->session->changes_made = 0;
115 // Last command in program
116                 if( saveas->quit_now )
117                         mwindow->quit();
118         }
119         return 1;
120 }
121
122 int Save::save_before_quit()
123 {
124         saveas->quit_now = 1;
125         handle_event();
126         return 0;
127 }
128
129 SaveAs::SaveAs(MWindow *mwindow)
130  : BC_MenuItem(_("Save as..."), "Shift-S", 'S'), Thread()
131 {
132         set_shift(1);
133         this->mwindow = mwindow;
134         quit_now = 0;
135 }
136
137 int SaveAs::set_mainmenu(MainMenu *mmenu)
138 {
139         this->mmenu = mmenu;
140         return 0;
141 }
142
143 int SaveAs::handle_event()
144 {
145         quit_now = 0;
146         start();
147         return 1;
148 }
149
150 void SaveAs::run()
151 {
152 // ======================================= get path from user
153         int result;
154 //printf("SaveAs::run 1\n");
155         char directory[1024], filename[1024];
156         sprintf(directory, "~");
157         mwindow->defaults->get("DIRECTORY", directory);
158
159 // Loop if file exists
160         do{
161                 SaveFileWindow *window;
162
163                 window = new SaveFileWindow(mwindow, directory);
164                 window->lock_window("SaveAs::run");
165                 window->create_objects();
166                 window->unlock_window();
167                 result = window->run_window();
168                 mwindow->defaults->update("DIRECTORY", window->get_submitted_path());
169                 strcpy(filename, window->get_submitted_path());
170                 delete window;
171
172 // Extend the filename with .xml
173                 if(strlen(filename) < 4 ||
174                         strcasecmp(&filename[strlen(filename) - 4], ".xml"))
175                 {
176                         strcat(filename, ".xml");
177                 }
178
179 // ======================================= try to save it
180                 if(filename[0] == 0) return;              // no filename given
181                 if(result == 1) return;          // user cancelled
182                 result = ConfirmSave::test_file(mwindow, filename);
183         }while(result);        // file exists so repeat
184
185 //printf("SaveAs::run 6 %s\n", filename);
186
187
188
189
190 // save it
191         FileXML file;
192         mwindow->gui->lock_window("SaveAs::run 1");
193 // update the project name
194         mwindow->set_filename(filename);
195         mwindow->edl->save_xml(&file, filename);
196         mwindow->gui->unlock_window();
197         file.terminate_string();
198
199         if(file.write_to_file(filename))
200         {
201                 char string2[256];
202                 mwindow->set_filename("");      // update the project name
203                 sprintf(string2, _("Couldn't open %s."), filename);
204                 ErrorBox error(_(PROGRAM_NAME ": Error"),
205                         mwindow->gui->get_abs_cursor_x(1),
206                         mwindow->gui->get_abs_cursor_y(1));
207                 error.create_objects(string2);
208                 error.raise_window();
209                 error.run_window();
210                 return;
211         }
212         else
213         {
214                 char string[BCTEXTLEN];
215                 sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
216                 mwindow->gui->lock_window("SaveAs::run 2");
217                 mwindow->gui->show_message(string);
218                 mwindow->gui->unlock_window();
219         }
220
221
222         mwindow->session->changes_made = 0;
223         mmenu->add_load(filename);
224 // Last command in program
225         if( quit_now )
226                 mwindow->quit();
227         return;
228 }
229
230
231
232
233
234
235
236
237 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
238  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
239         mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
240         init_directory,
241         _(PROGRAM_NAME ": Save"),
242         _("Enter a filename to save as"))
243 {
244         this->mwindow = mwindow;
245 }
246
247 SaveFileWindow::~SaveFileWindow() {}
248