Changeset View
Changeset View
Standalone View
Standalone View
source/blender/windowmanager/intern/wm_files.c
| Show First 20 Lines • Show All 1,938 Lines • ▼ Show 20 Lines | static bool wm_file_read_opwrap(bContext *C, | ||||
| return success; | return success; | ||||
| } | } | ||||
| /* currently fits in a pointer */ | /* currently fits in a pointer */ | ||||
| struct FileRuntime { | struct FileRuntime { | ||||
| bool is_untrusted; | bool is_untrusted; | ||||
| }; | }; | ||||
| static int wm_open_mainfile_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) | static void create_operator_state(wmOperatorType *ot, int first_state, int last_state) | ||||
| { | { | ||||
| PropertyRNA *prop = RNA_def_int(ot->srna, | |||||
| "state", | |||||
| first_state, | |||||
| first_state, | |||||
| last_state, | |||||
| "State", | |||||
| "", | |||||
| first_state, | |||||
| last_state); | |||||
| RNA_def_property_flag(prop, PROP_SKIP_SAVE); | |||||
brecht: Should also have PROP_HIDDEN. | |||||
| } | |||||
| static int get_operator_state(wmOperator *op) | |||||
| { | |||||
| return RNA_int_get(op->ptr, "state"); | |||||
| } | |||||
| static void set_operator_state(wmOperator *op, int state) | |||||
| { | |||||
| RNA_int_set(op->ptr, "state", state); | |||||
| } | |||||
| enum { | |||||
| OPEN_MAINFILE_STATE_DISCARD_CHANGES, | |||||
| OPEN_MAINFILE_STATE_SELECT_FILE_PATH, | |||||
| OPEN_MAINFILE_STATE_OPEN, | |||||
| }; | |||||
| static int wm_open_mainfile_exec(bContext *C, wmOperator *op); | |||||
| static int wm_open_mainfile_state__discard_changes(bContext *C, wmOperator *op) | |||||
| { | |||||
| BLI_assert(get_operator_state(op) == OPEN_MAINFILE_STATE_DISCARD_CHANGES); | |||||
| if (RNA_boolean_get(op->ptr, "display_file_selector")) { | |||||
| set_operator_state(op, OPEN_MAINFILE_STATE_SELECT_FILE_PATH); | |||||
| } | |||||
| else { | |||||
| set_operator_state(op, OPEN_MAINFILE_STATE_OPEN); | |||||
| } | |||||
| if (!RNA_boolean_get(op->ptr, "display_discard_warning")) { | |||||
| return wm_open_mainfile_exec(C, op); | |||||
| } | |||||
| wmWindowManager *wm = CTX_wm_manager(C); | |||||
| if (U.uiflag & USER_SAVE_PROMPT && !wm->file_saved) { | |||||
| return WM_operator_confirm_message(C, op, "Changes in current file will be lost. Continue?"); | |||||
| } | |||||
| else { | |||||
| return wm_open_mainfile_exec(C, op); | |||||
| } | |||||
| } | |||||
| static int wm_open_mainfile_state__select_file_path(bContext *C, wmOperator *op) | |||||
| { | |||||
| BLI_assert(get_operator_state(op) == OPEN_MAINFILE_STATE_SELECT_FILE_PATH); | |||||
| set_operator_state(op, OPEN_MAINFILE_STATE_OPEN); | |||||
| Main *bmain = CTX_data_main(C); | Main *bmain = CTX_data_main(C); | ||||
| const char *openname = BKE_main_blendfile_path(bmain); | const char *openname = BKE_main_blendfile_path(bmain); | ||||
| if (CTX_wm_window(C) == NULL) { | if (CTX_wm_window(C) == NULL) { | ||||
| /* in rare cases this could happen, when trying to invoke in background | /* in rare cases this could happen, when trying to invoke in background | ||||
| * mode on load for example. Don't use poll for this because exec() | * mode on load for example. Don't use poll for this because exec() | ||||
| * can still run without a window */ | * can still run without a window */ | ||||
| BKE_report(op->reports, RPT_ERROR, "Context window not set"); | BKE_report(op->reports, RPT_ERROR, "Context window not set"); | ||||
| Show All 11 Lines | static int wm_open_mainfile_state__select_file_path(bContext *C, wmOperator *op) | ||||
| wm_open_init_use_scripts(op, true); | wm_open_init_use_scripts(op, true); | ||||
| op->customdata = NULL; | op->customdata = NULL; | ||||
| WM_event_add_fileselect(C, op); | WM_event_add_fileselect(C, op); | ||||
| return OPERATOR_RUNNING_MODAL; | return OPERATOR_RUNNING_MODAL; | ||||
| } | } | ||||
| static int wm_open_mainfile_exec(bContext *C, wmOperator *op) | static int wm_open_mainfile_state__open(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| BLI_assert(get_operator_state(op) == OPEN_MAINFILE_STATE_OPEN); | |||||
| char filepath[FILE_MAX]; | char filepath[FILE_MAX]; | ||||
| bool success; | bool success; | ||||
| RNA_string_get(op->ptr, "filepath", filepath); | RNA_string_get(op->ptr, "filepath", filepath); | ||||
| /* re-use last loaded setting so we can reload a file without changing */ | /* re-use last loaded setting so we can reload a file without changing */ | ||||
| wm_open_init_load_ui(op, false); | wm_open_init_load_ui(op, false); | ||||
| wm_open_init_use_scripts(op, false); | wm_open_init_use_scripts(op, false); | ||||
| Show All 20 Lines | static int wm_open_mainfile_state__open(bContext *C, wmOperator *op) | ||||
| if (success) { | if (success) { | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| } | } | ||||
| else { | else { | ||||
| return OPERATOR_CANCELLED; | return OPERATOR_CANCELLED; | ||||
| } | } | ||||
| } | } | ||||
| static int wm_open_mainfile_exec(bContext *C, wmOperator *op) | |||||
| { | |||||
| int state = get_operator_state(op); | |||||
| switch (state) { | |||||
| case OPEN_MAINFILE_STATE_DISCARD_CHANGES: | |||||
| return wm_open_mainfile_state__discard_changes(C, op); | |||||
| case OPEN_MAINFILE_STATE_SELECT_FILE_PATH: | |||||
| return wm_open_mainfile_state__select_file_path(C, op); | |||||
| case OPEN_MAINFILE_STATE_OPEN: | |||||
| return wm_open_mainfile_state__open(C, op); | |||||
| default: | |||||
| BLI_assert(false); | |||||
| return OPERATOR_CANCELLED; | |||||
| } | |||||
| } | |||||
| static bool wm_open_mainfile_check(bContext *UNUSED(C), wmOperator *op) | static bool wm_open_mainfile_check(bContext *UNUSED(C), wmOperator *op) | ||||
| { | { | ||||
| struct FileRuntime *file_info = (struct FileRuntime *)&op->customdata; | struct FileRuntime *file_info = (struct FileRuntime *)&op->customdata; | ||||
| PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_scripts"); | PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_scripts"); | ||||
| bool is_untrusted = false; | bool is_untrusted = false; | ||||
| char path[FILE_MAX]; | char path[FILE_MAX]; | ||||
| char *lslash; | char *lslash; | ||||
| ▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| void WM_OT_open_mainfile(wmOperatorType *ot) | void WM_OT_open_mainfile(wmOperatorType *ot) | ||||
| { | { | ||||
| ot->name = "Open Blender File"; | ot->name = "Open Blender File"; | ||||
| ot->idname = "WM_OT_open_mainfile"; | ot->idname = "WM_OT_open_mainfile"; | ||||
| ot->description = "Open a Blender file"; | ot->description = "Open a Blender file"; | ||||
| ot->invoke = wm_open_mainfile_invoke; | |||||
| ot->exec = wm_open_mainfile_exec; | ot->exec = wm_open_mainfile_exec; | ||||
| ot->check = wm_open_mainfile_check; | ot->check = wm_open_mainfile_check; | ||||
| ot->ui = wm_open_mainfile_ui; | ot->ui = wm_open_mainfile_ui; | ||||
| /* omit window poll so this can work in background mode */ | /* omit window poll so this can work in background mode */ | ||||
| WM_operator_properties_filesel(ot, | WM_operator_properties_filesel(ot, | ||||
| FILE_TYPE_FOLDER | FILE_TYPE_BLENDER, | FILE_TYPE_FOLDER | FILE_TYPE_BLENDER, | ||||
| FILE_BLENDER, | FILE_BLENDER, | ||||
| FILE_OPENFILE, | FILE_OPENFILE, | ||||
| WM_FILESEL_FILEPATH, | WM_FILESEL_FILEPATH, | ||||
| FILE_DEFAULTDISPLAY, | FILE_DEFAULTDISPLAY, | ||||
| FILE_SORT_ALPHA); | FILE_SORT_ALPHA); | ||||
| RNA_def_boolean( | RNA_def_boolean( | ||||
| ot->srna, "load_ui", true, "Load UI", "Load user interface setup in the .blend file"); | ot->srna, "load_ui", true, "Load UI", "Load user interface setup in the .blend file"); | ||||
| RNA_def_boolean(ot->srna, | RNA_def_boolean(ot->srna, | ||||
| "use_scripts", | "use_scripts", | ||||
| true, | true, | ||||
| "Trusted Source", | "Trusted Source", | ||||
| "Allow .blend file to execute scripts automatically, default available from " | "Allow .blend file to execute scripts automatically, default available from " | ||||
| "system preferences"); | "system preferences"); | ||||
| create_operator_state(ot, OPEN_MAINFILE_STATE_DISCARD_CHANGES, OPEN_MAINFILE_STATE_OPEN); | |||||
| RNA_def_boolean(ot->srna, "display_discard_warning", true, "Display Discard Warning", ""); | |||||
| RNA_def_boolean(ot->srna, "display_file_selector", false, "Display File Selector", ""); | |||||
brechtUnsubmitted Not Done Inline ActionsThe default for this should be true. brecht: The default for this should be `true`. | |||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
| /** \name Reload (revert) main .blend file. | /** \name Reload (revert) main .blend file. | ||||
| * | * | ||||
| * \{ */ | * \{ */ | ||||
| ▲ Show 20 Lines • Show All 536 Lines • Show Last 20 Lines | |||||
Should also have PROP_HIDDEN.