4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
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.
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 Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation 2007
25 * ***** END GPL LICENSE BLOCK *****
28 /* placed up here because of crappy
35 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined */
37 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
39 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
40 #include "BLI_winstuff.h"
41 #include <process.h> /* getpid */
43 #include <unistd.h> /* getpid */
46 #include "MEM_guardedalloc.h"
47 #include "MEM_CacheLimiterC-Api.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_linklist.h"
52 #include "DNA_anim_types.h"
53 #include "DNA_ipo_types.h" // XXX old animation system
54 #include "DNA_object_types.h"
55 #include "DNA_space_types.h"
56 #include "DNA_userdef_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_windowmanager_types.h"
61 #include "BKE_blender.h"
62 #include "BKE_context.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_exotic.h"
66 #include "BKE_global.h"
67 #include "BKE_library.h"
69 #include "BKE_packedFile.h"
70 #include "BKE_report.h"
71 #include "BKE_texture.h"
72 #include "BKE_utildefines.h"
74 #include "BLO_readfile.h"
75 #include "BLO_writefile.h"
77 #include "RNA_access.h"
78 #include "RNA_define.h"
80 #include "ED_datafiles.h"
81 #include "ED_object.h"
82 #include "ED_screen.h"
85 #include "GHOST_C-api.h"
87 #include "UI_interface.h"
91 // XXX #include "BPY_extern.h"
96 #include "wm_window.h"
99 /* To be able to read files without windows closing, opening, moving
100 we try to prepare for worst case:
101 - active window gets active screen from file
102 - restoring the screens from non-active windows
103 Best case is all screens match, in that case they get assigned to proper window
105 static void wm_window_match_init(bContext *C, ListBase *wmlist)
107 wmWindowManager *wm= G.main->wm.first;
111 G.main->wm.first= G.main->wm.last= NULL;
113 /* first wrap up running stuff */
114 /* code copied from wm_init_exit.c */
115 for(wm= wmlist->first; wm; wm= wm->id.next) {
117 WM_jobs_stop_all(wm);
119 for(win= wm->windows.first; win; win= win->next) {
121 CTX_wm_window_set(C, win); /* needed by operator close callbacks */
122 ED_screen_exit(C, win, win->screen);
130 if(G.fileflags & G_FILE_NO_UI) return;
132 /* we take apart the used screens from non-active window */
133 for(win= wm->windows.first; win; win= win->next) {
134 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
135 if(win!=wm->winactive) {
136 BLI_remlink(&G.main->screen, win->screen);
137 //BLI_addtail(screenbase, win->screen);
142 /* match old WM with new, 4 cases:
143 1- no current wm, no read wm: make new default
144 2- no current wm, but read wm: that's OK, do nothing
145 3- current wm, but not in file: try match screen names
146 4- current wm, and wm in file: try match ghostwin
149 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
151 wmWindowManager *oldwm, *wm;
152 wmWindow *oldwin, *win;
155 if(oldwmlist->first==NULL) {
156 if(G.main->wm.first); /* nothing todo */
163 /* we've read file without wm..., keep current one entirely alive */
164 if(G.main->wm.first==NULL) {
165 bScreen *screen= CTX_wm_screen(C);
167 /* match oldwm to new dbase, only old files */
169 for(wm= oldwmlist->first; wm; wm= wm->id.next) {
171 for(win= wm->windows.first; win; win= win->next) {
172 /* all windows get active screen from file */
176 win->screen= ED_screen_duplicate(win, screen);
178 BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
179 win->screen->winid= win->winid;
183 G.main->wm= *oldwmlist;
185 /* screens were read from file! */
186 ED_screens_initialize(G.main->wm.first);
189 /* what if old was 3, and loaded 1? */
190 /* this code could move to setup_appdata */
191 oldwm= oldwmlist->first;
192 wm= G.main->wm.first;
194 /* ensure making new keymaps and set space types */
197 /* only first wm in list has ghostwins */
198 for(win= wm->windows.first; win; win= win->next) {
199 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
201 if(oldwin->winid == win->winid ) {
202 win->ghostwin= oldwin->ghostwin;
203 GHOST_SetWindowUserData(win->ghostwin, win); /* pointer back */
204 oldwin->ghostwin= NULL;
206 win->eventstate= oldwin->eventstate;
207 oldwin->eventstate= NULL;
209 /* ensure proper screen rescaling */
210 win->sizex= oldwin->sizex;
211 win->sizey= oldwin->sizey;
212 win->posx= oldwin->posx;
213 win->posy= oldwin->posy;
217 wm_close_and_free_all(C, oldwmlist);
222 /* in case UserDef was read, we re-initialize all, and do versioning */
223 static void wm_init_userdef()
226 MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
230 void WM_read_file(bContext *C, char *name, ReportList *reports)
234 /* first try to append data from exotic file formats... */
235 /* it throws error box when file doesnt exist and returns -1 */
236 /* note; it should set some error message somewhere... (ton) */
237 retval= BKE_read_exotic(CTX_data_scene(C), name);
239 /* we didn't succeed, now try to read Blender file */
243 /* put aside screens to match with persistant windows later */
244 /* also exit screens and editors */
245 wm_window_match_init(C, &wmbase);
247 retval= BKE_read_file(C, name, NULL, reports);
250 /* match the read WM with current WM */
251 wm_window_match_do(C, &wmbase);
252 wm_check(C); /* opens window(s), checks keymaps */
254 // XXX mainwindow_set_filename_to_title(G.main->name);
255 // XXX sound_initialize_sounds();
257 if(retval==2) wm_init_userdef(); // in case a userdef is read from regular .blend
259 if (retval!=0) G.relbase_valid = 1;
261 // XXX undo_editmode_clear();
263 BKE_write_undo(C, "original"); /* save current state */
265 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
266 // refresh_interface_font();
268 CTX_wm_window_set(C, NULL); /* exits queues */
271 BKE_write_undo(C, "Import file");
272 else if(retval == -1) {
273 if(reports && reports->list.first == NULL)
274 BKE_report(reports, RPT_ERROR, "Cannot read file.");
279 /* called on startup, (context entirely filled with NULLs) */
280 /* or called for 'Erase All' */
282 int WM_read_homefile(bContext *C, wmOperator *op)
285 char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
286 char *home= BLI_gethome();
287 int from_memory= op?RNA_boolean_get(op->ptr, "factory"):0;
292 free_ttfont(); /* still weird... what does it here? */
296 BLI_make_file_string(G.sce, tstr, home, ".B25.blend");
297 if(!BLI_exists(tstr))
298 BLI_make_file_string(G.sce, tstr, home, ".B.blend");
300 strcpy(scestr, G.sce); /* temporary store */
302 /* prevent loading no UI */
303 G.fileflags &= ~G_FILE_NO_UI;
305 /* put aside screens to match with persistant windows later */
306 wm_window_match_init(C, &wmbase);
308 if (!from_memory && BLI_exists(tstr)) {
309 success = BKE_read_file(C, tstr, NULL, NULL);
311 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL, NULL);
312 wm_clear_default_size(C);
315 /* match the read WM with current WM */
316 wm_window_match_do(C, &wmbase);
317 wm_check(C); /* opens window(s), checks keymaps */
319 strcpy(G.sce, scestr); /* restore */
323 /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
324 GPU_default_lights();
327 G.save_over = 0; // start with save preference untitled.blend
328 G.fileflags &= ~G_FILE_AUTOPLAY; /* disable autoplay in .B.blend... */
329 // mainwindow_set_filename_to_title(""); // empty string re-initializes title to "Blender"
331 // refresh_interface_font();
333 // undo_editmode_clear();
335 BKE_write_undo(C, "original"); /* save current state */
337 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
338 CTX_wm_window_set(C, NULL); /* exits queues */
344 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
349 char savedir[FILE_MAXDIR];
352 sprintf(pidstr, "%d.blend", abs(getpid()));
355 if (!BLI_exists(U.tempdir)) {
356 BLI_strncpy(subdir, "autosave", sizeof(subdir));
357 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
359 /* create a new autosave dir
360 * function already checks for existence or not */
361 BLI_recurdir_fileops(savedir);
363 BLI_make_file_string("/", buf, savedir, pidstr);
368 BLI_make_file_string("/", buf, U.tempdir, pidstr);
371 void WM_read_autosavefile(bContext *C)
373 char tstr[FILE_MAX], scestr[FILE_MAX];
376 BLI_strncpy(scestr, G.sce, FILE_MAX); /* temporal store */
378 get_autosave_location(tstr);
380 save_over = G.save_over;
381 BKE_read_file(C, tstr, NULL, NULL);
382 G.save_over = save_over;
383 BLI_strncpy(G.sce, scestr, FILE_MAX);
389 char name[FILE_MAX], filename[FILE_MAX];
391 struct RecentFile *recent;
395 BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
396 lines= BLI_read_file_as_lines(name);
398 G.recent_files.first = G.recent_files.last = NULL;
400 /* read list of recent opend files from .Blog to memory */
401 for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
403 if (!BLI_streq(line, "")) {
407 recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
408 BLI_addtail(&(G.recent_files), recent);
409 recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
410 recent->filename[0] = '\0';
412 strcpy(recent->filename, line);
417 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
419 BLI_free_file_lines(lines);
422 /* Add the drive names to the listing */
425 char folder[MAX_PATH];
429 tmp= GetLogicalDrives();
431 for (i=2; i < 26; i++) {
438 // XX fsmenu_insert_entry(tmps, 0, 0);
442 /* Adding Desktop and My Documents */
443 // XXX fsmenu_append_separator();
445 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
446 // XXX fsmenu_insert_entry(folder, 0, 0);
447 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
448 // XXX fsmenu_insert_entry(folder, 0, 0);
450 // XXX fsmenu_append_separator();
454 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
455 lines= BLI_read_file_as_lines(name);
457 for (l= lines; l; l= l->next) {
460 if (!BLI_streq(line, "")) {
461 // XXX fsmenu_insert_entry(line, 0, 1);
465 // XXX fsmenu_append_separator();
467 /* add last saved file */
468 BLI_split_dirfile(G.sce, name, filename); /* G.sce shouldn't be relative */
470 // XXX fsmenu_insert_entry(name, 0, 0);
472 BLI_free_file_lines(lines);
475 static void writeBlog(void)
477 struct RecentFile *recent, *next_recent;
478 char name[FILE_MAXDIR+FILE_MAXFILE];
482 BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
484 recent = G.recent_files.first;
485 /* refresh .Blog of recent opened files, when current file was changed */
486 if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
487 fp= fopen(name, "w");
489 /* add current file to the beginning of list */
490 recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
491 recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
492 recent->filename[0] = '\0';
493 strcpy(recent->filename, G.sce);
494 BLI_addhead(&(G.recent_files), recent);
495 /* write current file to .Blog */
496 fprintf(fp, "%s\n", recent->filename);
497 recent = recent->next;
499 /* write rest of recent opened files to .Blog */
500 while((i<U.recent_files) && (recent)){
501 /* this prevents to have duplicities in list */
502 if (strcmp(recent->filename, G.sce)!=0) {
503 fprintf(fp, "%s\n", recent->filename);
504 recent = recent->next;
507 next_recent = recent->next;
508 MEM_freeN(recent->filename);
509 BLI_freelinkN(&(G.recent_files), recent);
510 recent = next_recent;
519 static void do_history(char *name, ReportList *reports)
521 char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
522 int hisnr= U.versions;
524 if(U.versions==0) return;
525 if(strlen(name)<2) return;
528 sprintf(tempname1, "%s%d", name, hisnr-1);
529 sprintf(tempname2, "%s%d", name, hisnr);
531 if(BLI_rename(tempname1, tempname2))
532 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
537 /* is needed when hisnr==1 */
538 sprintf(tempname1, "%s%d", name, hisnr);
540 if(BLI_rename(name, tempname1))
541 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
544 void WM_write_file(bContext *C, char *target, ReportList *reports)
550 len = strlen(target);
552 if (len == 0) return;
553 if (len >= FILE_MAX) {
554 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
558 /* send the OnSave event */
559 // XXX if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&CTX_data_scene(C)->id, SCRIPT_ONSAVE);
561 for (li= G.main->library.first; li; li= li->id.next) {
562 if (BLI_streq(li->name, target)) {
563 BKE_report(reports, RPT_ERROR, "Cannot overwrite used library");
568 if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
569 sprintf(di, "%s.blend", target);
574 // if (BLI_exists(di)) {
575 // XXX if(!saveover(di))
579 if (G.fileflags & G_AUTOPACK) {
583 ED_object_exit_editmode(C, 0);
585 do_history(di, reports);
587 /* we use the UserDef to define compression flag */
588 writeflags= G.fileflags & ~G_FILE_COMPRESS;
589 if(U.flag & USER_FILECOMPRESS)
590 writeflags |= G_FILE_COMPRESS;
592 if (BLO_write_file(CTX_data_main(C), di, writeflags, reports)) {
595 strcpy(G.main->name, di); /* is guaranteed current file */
597 G.save_over = 1; /* disable untitled.blend convention */
602 // XXX waitcursor(0);
606 int WM_write_homefile(bContext *C, wmOperator *op)
608 char tstr[FILE_MAXDIR+FILE_MAXFILE];
611 BLI_make_file_string("/", tstr, BLI_gethome(), ".B25.blend");
613 /* force save as regular blend file */
614 write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
616 BLO_write_file(CTX_data_main(C), tstr, write_flags, op->reports);
620 return OPERATOR_FINISHED;
623 void WM_write_autosave(bContext *C)
625 char tstr[FILE_MAXDIR+FILE_MAXFILE];
628 get_autosave_location(tstr);
630 /* force save as regular blend file */
631 write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
633 /* error reporting to console */
634 BLO_write_file(CTX_data_main(C), tstr, write_flags, NULL);
637 /* if global undo; remove tempsave, otherwise rename */
638 void delete_autosave(void)
640 char tstr[FILE_MAXDIR+FILE_MAXFILE];
642 get_autosave_location(tstr);
644 if (BLI_exists(tstr)) {
645 char str[FILE_MAXDIR+FILE_MAXFILE];
646 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
648 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
649 else BLI_rename(tstr, str);