From d8c2709414ec3b25c2bdaeea57eb26c2ff91f977 Mon Sep 17 00:00:00 2001 From: Tamito Kajiyama Date: Sun, 2 Jun 2013 17:52:06 +0000 Subject: Better API design for making text datablocks after loading. An optional 'internal' argument was added to the bpy.data.texts.load() operator. The changes in revision 57153 were reverted, so that the is_in_memory and is_dirty properties of text datablocks are not editable again. In the C API layer, BKE_text_load_ex() was introduced to allow for optionally making text datablocks internal after loading. --- release/scripts/startup/bl_operators/freestyle.py | 6 +----- source/blender/blenkernel/BKE_text.h | 2 ++ source/blender/blenkernel/intern/text.c | 16 +++++++++++++--- source/blender/editors/space_text/text_ops.c | 9 +-------- source/blender/makesrna/intern/rna_main_api.c | 5 +++-- source/blender/makesrna/intern/rna_text.c | 2 ++ 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/release/scripts/startup/bl_operators/freestyle.py b/release/scripts/startup/bl_operators/freestyle.py index e9e636235ff..7aa6ae13b56 100644 --- a/release/scripts/startup/bl_operators/freestyle.py +++ b/release/scripts/startup/bl_operators/freestyle.py @@ -166,10 +166,6 @@ class SCENE_OT_freestyle_module_open(bpy.types.Operator): return {'RUNNING_MODAL'} def execute(self, context): - text = bpy.data.texts.load(self.filepath) - if self.make_internal: - text.is_in_memory = True - text.is_dirty = True - text.filepath = "" + text = bpy.data.texts.load(self.filepath, self.make_internal) self.freestyle_module.script = text return {'FINISHED'} diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index 8fd712bde72..2406fa51c84 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -48,6 +48,8 @@ int txt_get_undostate (void); struct Text *BKE_text_add (struct Main *bmain, const char *name); int txt_extended_ascii_as_utf8(char **str); int BKE_text_reload (struct Text *text); +struct Text *BKE_text_load_ex(struct Main *bmain, const char *file, const char *relpath, + const bool is_internal); struct Text *BKE_text_load (struct Main *bmain, const char *file, const char *relpath); struct Text *BKE_text_copy (struct Text *ta); void BKE_text_unlink (struct Main *bmain, struct Text *text); diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 470de8a205d..296f25e303e 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -362,7 +362,7 @@ int BKE_text_reload(Text *text) return 1; } -Text *BKE_text_load(Main *bmain, const char *file, const char *relpath) +Text *BKE_text_load_ex(Main *bmain, const char *file, const char *relpath, const bool is_internal) { FILE *fp; int i, llen, len; @@ -392,8 +392,13 @@ Text *BKE_text_load(Main *bmain, const char *file, const char *relpath) len = ftell(fp); fseek(fp, 0L, SEEK_SET); - ta->name = MEM_mallocN(strlen(file) + 1, "text_name"); - strcpy(ta->name, file); + if (is_internal == false) { + ta->name = MEM_mallocN(strlen(file) + 1, "text_name"); + strcpy(ta->name, file); + } + else { + ta->flags |= TXT_ISMEM | TXT_ISDIRTY; + } init_undo_text(ta); @@ -460,6 +465,11 @@ Text *BKE_text_load(Main *bmain, const char *file, const char *relpath) return ta; } +Text *BKE_text_load(Main *bmain, const char *file, const char *relpath) +{ + return BKE_text_load_ex(bmain, file, relpath, false); +} + Text *BKE_text_copy(Text *ta) { Text *tan; diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 04675430339..ca6bab1d1da 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -238,7 +238,7 @@ static int text_open_exec(bContext *C, wmOperator *op) RNA_string_get(op->ptr, "filepath", str); - text = BKE_text_load(bmain, str, G.main->name); + text = BKE_text_load_ex(bmain, str, G.main->name, internal); if (!text) { if (op->customdata) MEM_freeN(op->customdata); @@ -264,13 +264,6 @@ static int text_open_exec(bContext *C, wmOperator *op) st->text = text; st->top = 0; } - - if (internal) { - if (text->name) - MEM_freeN(text->name); - - text->name = NULL; - } text_drawcache_tag_update(st, 1); WM_event_add_notifier(C, NC_TEXT | NA_ADDED, text); diff --git a/source/blender/makesrna/intern/rna_main_api.c b/source/blender/makesrna/intern/rna_main_api.c index 1e77a799cd4..4c652770e3f 100644 --- a/source/blender/makesrna/intern/rna_main_api.c +++ b/source/blender/makesrna/intern/rna_main_api.c @@ -725,12 +725,12 @@ static void rna_Main_texts_remove(Main *bmain, PointerRNA *text_ptr) RNA_POINTER_INVALIDATE(text_ptr); } -static Text *rna_Main_texts_load(Main *bmain, ReportList *reports, const char *filepath) +static Text *rna_Main_texts_load(Main *bmain, ReportList *reports, const char *filepath, int is_internal) { Text *txt; errno = 0; - txt = BKE_text_load(bmain, filepath, bmain->name); + txt = BKE_text_load_ex(bmain, filepath, bmain->name, is_internal); if (!txt) BKE_reportf(reports, RPT_ERROR, "Cannot read '%s': %s", filepath, @@ -1701,6 +1701,7 @@ void RNA_def_main_texts(BlenderRNA *brna, PropertyRNA *cprop) RNA_def_function_ui_description(func, "Add a new text to the main database from a file"); parm = RNA_def_string_file_path(func, "filepath", "Path", FILE_MAX, "", "path for the datablock"); RNA_def_property_flag(parm, PROP_REQUIRED); + parm = RNA_def_boolean(func, "internal", 0, "Make internal", "Make text file internal after loading"); /* return type */ parm = RNA_def_pointer(func, "text", "Text", "", "New text datablock"); RNA_def_function_return(func, parm); diff --git a/source/blender/makesrna/intern/rna_text.c b/source/blender/makesrna/intern/rna_text.c index b1eb0bb2228..df6181af4b2 100644 --- a/source/blender/makesrna/intern/rna_text.c +++ b/source/blender/makesrna/intern/rna_text.c @@ -148,6 +148,7 @@ static void rna_def_text(BlenderRNA *brna) prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE); RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISDIRTY); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); RNA_def_property_ui_text(prop, "Dirty", "Text file has been edited since last save"); prop = RNA_def_property(srna, "is_modified", PROP_BOOLEAN, PROP_NONE); @@ -157,6 +158,7 @@ static void rna_def_text(BlenderRNA *brna) prop = RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE); RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISMEM); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); RNA_def_property_ui_text(prop, "Memory", "Text file is in memory, without a corresponding file on disk"); prop = RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE); -- cgit v1.2.3