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.
This commit is contained in:
2013-06-02 17:52:06 +00:00
parent 5506ab080d
commit d8c2709414
6 changed files with 22 additions and 18 deletions

View File

@@ -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'}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);