diff options
Diffstat (limited to 'source/blender/windowmanager/intern/wm_files.c')
-rw-r--r-- | source/blender/windowmanager/intern/wm_files.c | 273 |
1 files changed, 169 insertions, 104 deletions
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c index cc81e4f2715..ed1b29d61ce 100644 --- a/source/blender/windowmanager/intern/wm_files.c +++ b/source/blender/windowmanager/intern/wm_files.c @@ -141,11 +141,37 @@ static void wm_history_file_free(RecentFile *recent); static void wm_history_file_update(void); static void wm_history_file_write(void); -/* To be able to read files without windows closing, opening, moving +/* -------------------------------------------------------------------- */ +/** \name Misc Utility Functions + * \{ */ + +void WM_file_tag_modified(void) +{ + wmWindowManager *wm = G_MAIN->wm.first; + if (wm->file_saved) { + wm->file_saved = 0; + /* notifier that data changed, for save-over warning or header */ + WM_main_add_notifier(NC_WM | ND_DATACHANGED, NULL); + } +} + +bool wm_file_or_image_is_modified(const Main *bmain, const wmWindowManager *wm) +{ + return !wm->file_saved || ED_image_should_save_modified(bmain); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Window Matching for File Reading + * \{ */ + +/** + * To be able to read files without windows closing, opening, moving * we try to prepare for worst case: * - active window gets active screen from file * - restoring the screens from non-active windows - * Best case is all screens match, in that case they get assigned to proper window + * Best case is all screens match, in that case they get assigned to proper window. */ static void wm_window_match_init(bContext *C, ListBase *wmlist) { @@ -355,6 +381,12 @@ static void wm_window_match_do(bContext *C, } } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Preferences Initialization & Versioning + * \{ */ + /* in case UserDef was read, we re-initialize all, and do versioning */ static void wm_init_userdef(Main *bmain) { @@ -389,6 +421,15 @@ static void wm_init_userdef(Main *bmain) # define BKE_READ_EXOTIC_OK_OTHER 1 /* other supported formats */ #endif +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read Exotic File Formats + * + * Currently only supports '.blend' files, + * we could support registering other file formats and their loaders. + * \{ */ + /* intended to check for non-blender formats but for now it only reads blends */ static int wm_read_exotic(const char *name) { @@ -441,6 +482,12 @@ static int wm_read_exotic(const char *name) return retval; } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read Blend-File Shared Utilities + * \{ */ + void WM_file_autoexec_init(const char *filepath) { if (G.f & G_FLAG_SCRIPT_OVERRIDE_PREF) { @@ -486,6 +533,24 @@ void wm_file_read_report(bContext *C, Main *bmain) /** * Logic shared between #WM_file_read & #wm_homefile_read, + * call before loading a file. + * \note In the case of #WM_file_read the file may fail to load. + * Change here shouldn't cause user-visible changes in that case. + */ +static void wm_file_read_pre(bContext *C, bool use_data, bool UNUSED(use_userdef)) +{ + if (use_data) { + BKE_callback_exec_null(CTX_data_main(C), BKE_CB_EVT_LOAD_PRE); + BLI_timer_on_file_load(); + } + + /* Always do this as both startup and preferences may have loaded in many font's + * at a different zoom level to the file being loaded. */ + UI_view2d_zoom_cache_reset(); +} + +/** + * Logic shared between #WM_file_read & #wm_homefile_read, * updates to make after reading a file. */ static void wm_file_read_post(bContext *C, @@ -510,12 +575,16 @@ static void wm_file_read_post(bContext *C, if (is_startup_file) { /* possible python hasn't been initialized */ if (CTX_py_init_get(C)) { - if (reset_app_template) { + bool reset_all = use_userdef; + if (use_userdef || reset_app_template) { /* Only run when we have a template path found. */ if (BKE_appdir_app_template_any()) { BPY_execute_string( C, (const char *[]){"bl_app_template_utils", NULL}, "bl_app_template_utils.reset()"); + reset_all = true; } + } + if (reset_all) { /* sync addons, these may have changed from the defaults */ BPY_execute_string(C, (const char *[]){"addon_utils", NULL}, "addon_utils.reset_all()"); } @@ -601,21 +670,27 @@ static void wm_file_read_post(bContext *C, } } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read Main Blend-File API + * \{ */ + bool WM_file_read(bContext *C, const char *filepath, ReportList *reports) { /* assume automated tasks with background, don't write recent file list */ const bool do_history = (G.background == false) && (CTX_wm_manager(C)->op_undo_depth == 0); bool success = false; + const bool use_data = true; + const bool use_userdef = false; + /* so we can get the error message */ errno = 0; WM_cursor_wait(1); - BKE_callback_exec_null(CTX_data_main(C), BKE_CB_EVT_LOAD_PRE); - BLI_timer_on_file_load(); - - UI_view2d_zoom_cache_reset(); + wm_file_read_pre(C, use_data, use_userdef); /* first try to append data from exotic file formats... */ /* it throws error box when file doesn't exist and returns -1 */ @@ -675,8 +750,6 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports) } } - const bool use_data = true; - const bool use_userdef = false; wm_file_read_post(C, false, false, use_data, use_userdef, false); } #if 0 @@ -746,6 +819,12 @@ const char *WM_init_state_app_template_get(void) return wm_init_state_app_template.override ? wm_init_state_app_template.app_template : NULL; } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read Startup & Preferences Blend-File API + * \{ */ + /** * Called on startup, (context entirely filled with NULLs) * or called for 'New File' both startup.blend and userpref.blend are checked. @@ -802,6 +881,23 @@ void wm_homefile_read(bContext *C, * or use app-template startup.blend which the user hasn't saved. */ bool is_factory_startup = true; + const char *app_template = NULL; + bool update_defaults = false; + + if (filepath_startup_override != NULL) { + /* pass */ + } + else if (app_template_override) { + /* This may be clearing the current template by setting to an empty string. */ + app_template = app_template_override; + } + else if (!use_factory_settings && U.app_template[0]) { + app_template = U.app_template; + } + + const bool reset_app_template = ((!app_template && U.app_template[0]) || + (app_template && !STREQ(app_template, U.app_template))); + /* options exclude eachother */ BLI_assert((use_factory_settings && filepath_startup_override) == 0); @@ -809,18 +905,30 @@ void wm_homefile_read(bContext *C, SET_FLAG_FROM_TEST(G.f, (U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0, G_FLAG_SCRIPT_AUTOEXEC); } - if (use_data) { - BKE_callback_exec_null(CTX_data_main(C), BKE_CB_EVT_LOAD_PRE); - BLI_timer_on_file_load(); + if (use_userdef || reset_app_template) { +#ifdef WITH_PYTHON + /* This only runs once Blender has already started. */ + if (CTX_py_init_get(C)) { + /* This is restored by 'wm_file_read_post', disable before loading any preferences + * so an add-on can read their own preferences when un-registering, + * and use new preferences if/when re-registering, see T67577. + * + * Note that this fits into 'wm_file_read_pre' function but gets messy + * since we need to know if 'reset_app_template' is true. */ + BPY_execute_string(C, (const char *[]){"addon_utils", NULL}, "addon_utils.disable_all()"); + } +#endif /* WITH_PYTHON */ + } + + wm_file_read_pre(C, use_data, use_userdef); + if (use_data) { G.relbase_valid = 0; /* put aside screens to match with persistent windows later */ wm_window_match_init(C, &wmbase); } - UI_view2d_zoom_cache_reset(); - filepath_startup[0] = '\0'; filepath_userdef[0] = '\0'; app_template_system[0] = '\0'; @@ -861,28 +969,6 @@ void wm_homefile_read(bContext *C, } } - const char *app_template = NULL; - bool update_defaults = false; - bool reset_app_template = false; - - if (filepath_startup_override != NULL) { - /* pass */ - } - else if (app_template_override) { - /* This may be clearing the current template by setting to an empty string. */ - app_template = app_template_override; - } - else if (!use_factory_settings && U.app_template[0]) { - app_template = U.app_template; - } - - if ((!app_template && U.app_template[0]) || - (app_template && !STREQ(app_template, U.app_template))) { - /* Always load UI when switching to another template. */ - G.fileflags &= ~G_FILE_NO_UI; - reset_app_template = true; - } - if ((app_template != NULL) && (app_template[0] != '\0')) { if (!BKE_appdir_app_template_id_search( app_template, app_template_system, sizeof(app_template_system))) { @@ -1028,6 +1114,11 @@ void wm_homefile_read(bContext *C, * Screws up autosaves otherwise can remove this eventually, * only in a 2.53 and older, now its not written. */ G.fileflags &= ~G_FILE_RELATIVE_REMAP; + + if (reset_app_template) { + /* Always load UI when switching to another template. */ + G.fileflags &= ~G_FILE_NO_UI; + } } bmain = CTX_data_main(C); @@ -1035,7 +1126,6 @@ void wm_homefile_read(bContext *C, if (use_userdef) { /* check userdef before open window, keymaps etc */ wm_init_userdef(bmain); - reset_app_template = true; } if (use_data) { @@ -1070,7 +1160,7 @@ void wm_homefile_read(bContext *C, } /* -------------------------------------------------------------------- */ -/** \name WM History File API +/** \name Blend-File History API * \{ */ void wm_history_file_read(void) @@ -1199,7 +1289,7 @@ static void wm_history_file_update(void) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Save Main .blend File (internal) +/** \name Save Main Blend-File (internal) * \{ */ /* screen can be NULL */ @@ -1623,18 +1713,8 @@ void wm_open_init_use_scripts(wmOperator *op, bool use_prefs) /** \} */ -void WM_file_tag_modified(void) -{ - wmWindowManager *wm = G_MAIN->wm.first; - if (wm->file_saved) { - wm->file_saved = 0; - /* notifier that data changed, for save-over warning or header */ - WM_main_add_notifier(NC_WM | ND_DATACHANGED, NULL); - } -} - /* -------------------------------------------------------------------- */ -/** \name Preferences/startup save & load. +/** \name Startup File Save Operator * \{ */ /** @@ -1699,48 +1779,11 @@ void WM_OT_save_homefile(wmOperatorType *ot) ot->exec = wm_homefile_write_exec; } -static int wm_userpref_autoexec_add_exec(bContext *UNUSED(C), wmOperator *UNUSED(op)) -{ - bPathCompare *path_cmp = MEM_callocN(sizeof(bPathCompare), "bPathCompare"); - BLI_addtail(&U.autoexec_paths, path_cmp); - U.runtime.is_dirty = true; - return OPERATOR_FINISHED; -} - -void WM_OT_userpref_autoexec_path_add(wmOperatorType *ot) -{ - ot->name = "Add Autoexec Path"; - ot->idname = "WM_OT_userpref_autoexec_path_add"; - ot->description = "Add path to exclude from autoexecution"; - - ot->exec = wm_userpref_autoexec_add_exec; - - ot->flag = OPTYPE_INTERNAL; -} - -static int wm_userpref_autoexec_remove_exec(bContext *UNUSED(C), wmOperator *op) -{ - const int index = RNA_int_get(op->ptr, "index"); - bPathCompare *path_cmp = BLI_findlink(&U.autoexec_paths, index); - if (path_cmp) { - BLI_freelinkN(&U.autoexec_paths, path_cmp); - U.runtime.is_dirty = true; - } - return OPERATOR_FINISHED; -} - -void WM_OT_userpref_autoexec_path_remove(wmOperatorType *ot) -{ - ot->name = "Remove Autoexec Path"; - ot->idname = "WM_OT_userpref_autoexec_path_remove"; - ot->description = "Remove path to exclude from autoexecution"; - - ot->exec = wm_userpref_autoexec_remove_exec; - - ot->flag = OPTYPE_INTERNAL; +/** \} */ - RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "", 0, 1000); -} +/* -------------------------------------------------------------------- */ +/** \name Write Preferences Operator + * \{ */ /* Only save the prefs block. operator entry */ static int wm_userpref_write_exec(bContext *C, wmOperator *op) @@ -1765,6 +1808,12 @@ void WM_OT_save_userpref(wmOperatorType *ot) ot->exec = wm_userpref_write_exec; } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read Preferences Operator + * \{ */ + /** * When reading preferences, there are some exceptions for values which are reset. */ @@ -1828,10 +1877,6 @@ static void wm_userpref_update_when_changed(bContext *C, rna_struct_update_when_changed(C, bmain, &ptr_a, &ptr_b); -#ifdef WITH_PYTHON - BPY_execute_string(C, (const char *[]){"addon_utils", NULL}, "addon_utils.reset_all()"); -#endif - WM_reinit_gizmomap_all(bmain); WM_keyconfig_reload(C); @@ -1897,6 +1942,12 @@ void WM_OT_read_factory_userpref(wmOperatorType *ot) ot->exec = wm_userpref_read_exec; } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read File History Operator + * \{ */ + static int wm_history_file_read_exec(bContext *UNUSED(C), wmOperator *UNUSED(op)) { ED_file_read_bookmarks(); @@ -1917,6 +1968,14 @@ void WM_OT_read_history(wmOperatorType *ot) ot->flag = OPTYPE_INTERNAL; } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Read Startup & Preferences Operator + * + * Both #WM_OT_read_homefile & #WM_OT_read_factory_settings. + * \{ */ + static int wm_homefile_read_exec(bContext *C, wmOperator *op) { const bool use_factory_settings = (STREQ(op->type->idname, "WM_OT_read_factory_settings")); @@ -2442,7 +2501,7 @@ void WM_OT_revert_mainfile(wmOperatorType *ot) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Recover last session & auto-save. +/** \name Recover Last Session Operator * \{ */ void WM_recover_last_session(bContext *C, ReportList *reports) @@ -2486,6 +2545,12 @@ void WM_OT_recover_last_session(wmOperatorType *ot) ot->exec = wm_recover_last_session_exec; } +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Auto-Save Main .blend File Operator + * \{ */ + static int wm_recover_auto_save_exec(bContext *C, wmOperator *op) { char filepath[FILE_MAX]; @@ -2540,6 +2605,8 @@ void WM_OT_recover_auto_save(wmOperatorType *ot) /* -------------------------------------------------------------------- */ /** \name Save Main .blend File Operator + * + * Both #WM_OT_save_as_mainfile & #WM_OT_save_mainfile. * \{ */ static void wm_filepath_default(char *filepath) @@ -2766,7 +2833,7 @@ void WM_OT_save_mainfile(wmOperatorType *ot) /** \} */ /* -------------------------------------------------------------------- */ -/** \name Auto-execution of scripts warning popup +/** \name Auto Script Execution Warning Dialog * \{ */ static void wm_block_autorun_warning_ignore(bContext *C, void *arg_block, void *UNUSED(arg)) @@ -2968,8 +3035,11 @@ void wm_test_autorun_warning(bContext *C) } } -/* Close File Dialog - *************************************/ +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Close File Dialog + * \{ */ static char save_images_when_file_is_closed = true; @@ -3235,9 +3305,4 @@ void wm_close_file_dialog(bContext *C, wmGenericCallback *post_action) } } -bool wm_file_or_image_is_modified(const Main *bmain, const wmWindowManager *wm) -{ - return !wm->file_saved || ED_image_should_save_modified(bmain); -} - /** \} */ |