prevent popup deactivation while button_down
[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"))
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, 
93                         mwindow->session->filename,
94                         0,
95                         0);
96                 file.terminate_string();
97
98                 if(file.write_to_file(mwindow->session->filename))
99                 {
100                         char string2[256];
101                         sprintf(string2, _("Couldn't open %s"), mwindow->session->filename);
102                         ErrorBox error(_(PROGRAM_NAME ": Error"),
103                                 mwindow->gui->get_abs_cursor_x(1),
104                                 mwindow->gui->get_abs_cursor_y(1));
105                         error.create_objects(string2);
106                         error.raise_window();
107                         error.run_window();
108                         return 1;               
109                 }
110                 else
111                 {
112                         char string[BCTEXTLEN];
113                         sprintf(string, _("\"%s\" %dC written"),
114                                  mwindow->session->filename, (int)strlen(file.string()));
115                         mwindow->gui->show_message(string);
116                 }
117                 mwindow->session->changes_made = 0;
118 // Last command in program
119 //              if(saveas->quit_now) mwindow->gui->set_done(0);
120                 if(saveas->quit_now) mwindow->playback_3d->quit();
121         }
122         return 1;
123 }
124
125 int Save::save_before_quit()
126 {
127         saveas->quit_now = 1;
128         handle_event();
129         return 0;
130 }
131
132 SaveAs::SaveAs(MWindow *mwindow)
133  : BC_MenuItem(_("Save as..."), ""), Thread()
134
135         this->mwindow = mwindow; 
136         quit_now = 0;
137 }
138
139 int SaveAs::set_mainmenu(MainMenu *mmenu)
140 {
141         this->mmenu = mmenu;
142         return 0;
143 }
144
145 int SaveAs::handle_event() 
146
147         quit_now = 0;
148         start();
149         return 1;
150 }
151
152 void SaveAs::run()
153 {
154 // ======================================= get path from user
155         int result;
156 //printf("SaveAs::run 1\n");
157         char directory[1024], filename[1024];
158         sprintf(directory, "~");
159         mwindow->defaults->get("DIRECTORY", directory);
160
161 // Loop if file exists
162         do{
163                 SaveFileWindow *window;
164
165                 window = new SaveFileWindow(mwindow, directory);
166                 window->lock_window("SaveAs::run");
167                 window->create_objects();
168                 window->unlock_window();
169                 result = window->run_window();
170                 mwindow->defaults->update("DIRECTORY", window->get_submitted_path());
171                 strcpy(filename, window->get_submitted_path());
172                 delete window;
173
174 // Extend the filename with .xml
175                 if(strlen(filename) < 4 || 
176                         strcasecmp(&filename[strlen(filename) - 4], ".xml"))
177                 {
178                         strcat(filename, ".xml");
179                 }
180
181 // ======================================= try to save it
182                 if(filename[0] == 0) return;              // no filename given
183                 if(result == 1) return;          // user cancelled
184                 result = ConfirmSave::test_file(mwindow, filename);
185         }while(result);        // file exists so repeat
186
187 //printf("SaveAs::run 6 %s\n", filename);
188
189
190
191
192 // save it
193         FileXML file;
194         mwindow->gui->lock_window("SaveAs::run 1");
195 // update the project name
196         mwindow->set_filename(filename);      
197         mwindow->edl->save_xml(&file, 
198                 filename,
199                 0,
200                 0);
201         mwindow->gui->unlock_window();
202         file.terminate_string();
203
204         if(file.write_to_file(filename))
205         {
206                 char string2[256];
207                 mwindow->set_filename("");      // update the project name
208                 sprintf(string2, _("Couldn't open %s."), filename);
209                 ErrorBox error(_(PROGRAM_NAME ": Error"),
210                         mwindow->gui->get_abs_cursor_x(1),
211                         mwindow->gui->get_abs_cursor_y(1));
212                 error.create_objects(string2);
213                 error.raise_window();
214                 error.run_window();
215                 return;         
216         }
217         else
218         {
219                 char string[BCTEXTLEN];
220                 sprintf(string, _("\"%s\" %dC written"), filename, (int)strlen(file.string()));
221                 mwindow->gui->lock_window("SaveAs::run 2");
222                 mwindow->gui->show_message(string);
223                 mwindow->gui->unlock_window();
224         }
225
226
227         mwindow->session->changes_made = 0;
228         mmenu->add_load(filename);
229 // Last command in program
230 //      if(quit_now) mwindow->gui->set_done(0);
231         if(quit_now) mwindow->playback_3d->quit();
232         return;
233 }
234
235
236
237
238
239
240
241
242 SaveFileWindow::SaveFileWindow(MWindow *mwindow, char *init_directory)
243  : BC_FileBox(mwindow->gui->get_abs_cursor_x(1),
244         mwindow->gui->get_abs_cursor_y(1) - BC_WindowBase::get_resources()->filebox_h / 2,
245         init_directory, 
246         _(PROGRAM_NAME ": Save"), 
247         _("Enter a filename to save as"))
248
249         this->mwindow = mwindow; 
250 }
251
252 SaveFileWindow::~SaveFileWindow() {}
253