2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-12-31 17:12:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-12-31 17:12:17 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup RNA
|
2011-02-27 20:20:01 +00:00
|
|
|
*/
|
|
|
|
|
|
2008-12-31 17:12:17 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
2009-01-08 13:57:29 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
#include "BLT_translation.h"
|
2013-01-05 12:06:50 +00:00
|
|
|
|
2008-12-31 17:12:17 +00:00
|
|
|
#include "BKE_text.h"
|
|
|
|
|
|
2019-08-13 15:35:48 +02:00
|
|
|
#include "ED_text.h"
|
|
|
|
|
|
2008-12-31 17:12:17 +00:00
|
|
|
#include "RNA_define.h"
|
|
|
|
|
|
|
|
|
|
#include "rna_internal.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_text_types.h"
|
|
|
|
|
|
2009-03-13 13:38:41 +00:00
|
|
|
#include "WM_types.h"
|
|
|
|
|
|
2008-12-31 17:12:17 +00:00
|
|
|
#ifdef RNA_RUNTIME
|
|
|
|
|
|
|
|
|
|
static void rna_Text_filename_get(PointerRNA *ptr, char *value)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Text *text = (Text *)ptr->data;
|
2008-12-31 17:12:17 +00:00
|
|
|
|
2019-06-04 00:21:57 +10:00
|
|
|
if (text->name) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcpy(value, text->name);
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2019-04-17 06:17:24 +02:00
|
|
|
value[0] = '\0';
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rna_Text_filename_length(PointerRNA *ptr)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Text *text = (Text *)ptr->data;
|
|
|
|
|
return (text->name) ? strlen(text->name) : 0;
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rna_Text_filename_set(PointerRNA *ptr, const char *value)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Text *text = (Text *)ptr->data;
|
2008-12-31 17:12:17 +00:00
|
|
|
|
2019-06-04 00:21:57 +10:00
|
|
|
if (text->name) {
|
2019-04-17 06:17:24 +02:00
|
|
|
MEM_freeN(text->name);
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
2008-12-31 17:12:17 +00:00
|
|
|
|
2019-06-04 00:21:57 +10:00
|
|
|
if (value[0]) {
|
2019-04-17 06:17:24 +02:00
|
|
|
text->name = BLI_strdup(value);
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2019-04-17 06:17:24 +02:00
|
|
|
text->name = NULL;
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-05 22:25:34 +02:00
|
|
|
static bool rna_Text_modified_get(PointerRNA *ptr)
|
2009-04-22 18:39:44 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Text *text = (Text *)ptr->data;
|
|
|
|
|
return BKE_text_file_modified_check(text) != 0;
|
2009-04-22 18:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-11 07:23:23 +00:00
|
|
|
static int rna_Text_current_line_index_get(PointerRNA *ptr)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
Text *text = (Text *)ptr->data;
|
|
|
|
|
return BLI_findindex(&text->lines, text->curl);
|
2013-09-11 07:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
2014-01-05 01:24:10 +11:00
|
|
|
static void rna_Text_current_line_index_set(PointerRNA *ptr, int value)
|
|
|
|
|
{
|
2019-10-04 06:30:45 +10:00
|
|
|
Text *text = ptr->data;
|
|
|
|
|
TextLine *line = BLI_findlink(&text->lines, value);
|
|
|
|
|
if (line == NULL) {
|
|
|
|
|
line = text->lines.last;
|
|
|
|
|
}
|
|
|
|
|
text->curl = line;
|
|
|
|
|
text->curc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rna_Text_select_end_line_index_get(PointerRNA *ptr)
|
|
|
|
|
{
|
|
|
|
|
Text *text = ptr->data;
|
|
|
|
|
return BLI_findindex(&text->lines, text->sell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rna_Text_select_end_line_index_set(PointerRNA *ptr, int value)
|
|
|
|
|
{
|
|
|
|
|
Text *text = ptr->data;
|
|
|
|
|
TextLine *line = BLI_findlink(&text->lines, value);
|
|
|
|
|
if (line == NULL) {
|
|
|
|
|
line = text->lines.last;
|
|
|
|
|
}
|
|
|
|
|
text->sell = line;
|
|
|
|
|
text->selc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rna_Text_current_character_get(PointerRNA *ptr)
|
|
|
|
|
{
|
|
|
|
|
Text *text = ptr->data;
|
|
|
|
|
return BLI_str_utf8_offset_to_index(text->curl->line, text->curc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rna_Text_current_character_set(PointerRNA *ptr, int index)
|
|
|
|
|
{
|
|
|
|
|
Text *text = ptr->data;
|
|
|
|
|
TextLine *line = text->curl;
|
|
|
|
|
const int len_utf8 = BLI_strlen_utf8(line->line);
|
|
|
|
|
CLAMP_MAX(index, len_utf8);
|
|
|
|
|
text->curc = BLI_str_utf8_offset_from_index(line->line, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rna_Text_select_end_character_get(PointerRNA *ptr)
|
|
|
|
|
{
|
|
|
|
|
Text *text = ptr->data;
|
|
|
|
|
return BLI_str_utf8_offset_to_index(text->sell->line, text->selc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rna_Text_select_end_character_set(PointerRNA *ptr, int index)
|
|
|
|
|
{
|
|
|
|
|
Text *text = ptr->data;
|
|
|
|
|
TextLine *line = text->sell;
|
|
|
|
|
const int len_utf8 = BLI_strlen_utf8(line->line);
|
|
|
|
|
CLAMP_MAX(index, len_utf8);
|
|
|
|
|
text->selc = BLI_str_utf8_offset_from_index(line->line, index);
|
2014-01-05 01:24:10 +11:00
|
|
|
}
|
|
|
|
|
|
2010-08-11 05:21:43 +00:00
|
|
|
static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
|
2008-12-31 17:12:17 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextLine *line = (TextLine *)ptr->data;
|
2008-12-31 17:12:17 +00:00
|
|
|
|
2019-06-04 00:21:57 +10:00
|
|
|
if (line->line) {
|
2019-04-17 06:17:24 +02:00
|
|
|
strcpy(value, line->line);
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2019-04-17 06:17:24 +02:00
|
|
|
value[0] = '\0';
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-11 05:21:43 +00:00
|
|
|
static int rna_TextLine_body_length(PointerRNA *ptr)
|
2008-12-31 17:12:17 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextLine *line = (TextLine *)ptr->data;
|
|
|
|
|
return line->len;
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-11 05:21:43 +00:00
|
|
|
static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
|
2008-12-31 17:12:17 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextLine *line = (TextLine *)ptr->data;
|
|
|
|
|
int len = strlen(value);
|
2008-12-31 17:12:17 +00:00
|
|
|
|
2019-06-04 00:21:57 +10:00
|
|
|
if (line->line) {
|
2019-04-17 06:17:24 +02:00
|
|
|
MEM_freeN(line->line);
|
2019-06-04 00:21:57 +10:00
|
|
|
}
|
2010-08-11 05:21:43 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
line->line = MEM_mallocN((len + 1) * sizeof(char), "rna_text_body");
|
|
|
|
|
line->len = len;
|
|
|
|
|
memcpy(line->line, value, len + 1);
|
2009-03-13 13:38:41 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (line->format) {
|
|
|
|
|
MEM_freeN(line->format);
|
|
|
|
|
line->format = NULL;
|
|
|
|
|
}
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
static void rna_def_text_line(BlenderRNA *brna)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
StructRNA *srna;
|
|
|
|
|
PropertyRNA *prop;
|
|
|
|
|
|
|
|
|
|
srna = RNA_def_struct(brna, "TextLine", NULL);
|
|
|
|
|
RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text data-block");
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
|
|
|
|
|
RNA_def_property_string_funcs(
|
|
|
|
|
prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
|
|
|
|
|
RNA_def_property_ui_text(prop, "Line", "Text in the line");
|
|
|
|
|
RNA_def_property_update(prop, NC_TEXT | NA_EDITED, NULL);
|
|
|
|
|
RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_TEXT);
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rna_def_text(BlenderRNA *brna)
|
|
|
|
|
{
|
2019-08-04 12:51:28 +02:00
|
|
|
|
|
|
|
|
static const EnumPropertyItem indentation_items[] = {
|
|
|
|
|
{0, "TABS", 0, "Tabs", "Indent using tabs"},
|
|
|
|
|
{TXT_TABSTOSPACES, "SPACES", 0, "Spaces", "Indent using spaces"},
|
|
|
|
|
{0, NULL, 0, NULL, NULL},
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
StructRNA *srna;
|
|
|
|
|
PropertyRNA *prop;
|
|
|
|
|
|
|
|
|
|
srna = RNA_def_struct(brna, "Text", "ID");
|
|
|
|
|
RNA_def_struct_ui_text(
|
|
|
|
|
srna, "Text", "Text data-block referencing an external or packed text file");
|
|
|
|
|
RNA_def_struct_ui_icon(srna, ICON_TEXT);
|
|
|
|
|
RNA_def_struct_clear_flag(srna, STRUCT_ID_REFCOUNT);
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_NONE);
|
|
|
|
|
RNA_def_property_string_funcs(
|
|
|
|
|
prop, "rna_Text_filename_get", "rna_Text_filename_length", "rna_Text_filename_set");
|
|
|
|
|
RNA_def_property_ui_text(prop, "File Path", "Filename of the text file");
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
|
|
|
|
|
RNA_def_property_boolean_funcs(prop, "rna_Text_modified_get", NULL);
|
|
|
|
|
RNA_def_property_ui_text(
|
|
|
|
|
prop, "Modified", "Text file on disk is different than the one in memory");
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISSCRIPT);
|
|
|
|
|
RNA_def_property_ui_text(
|
2019-04-18 14:45:23 +02:00
|
|
|
prop, "Register", "Run this text as a script on loading, Text name must end with \".py\"");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-04 12:51:28 +02:00
|
|
|
prop = RNA_def_property(srna, "indentation", PROP_ENUM, PROP_NONE); /* as an enum */
|
|
|
|
|
RNA_def_property_enum_bitflag_sdna(prop, NULL, "flags");
|
|
|
|
|
RNA_def_property_enum_items(prop, indentation_items);
|
|
|
|
|
RNA_def_property_ui_text(prop, "Indentation", "Use tabs or spaces for indentation");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
|
|
|
|
|
RNA_def_property_struct_type(prop, "TextLine");
|
|
|
|
|
RNA_def_property_ui_text(prop, "Lines", "Lines of text");
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
|
|
|
|
|
RNA_def_property_flag(prop, PROP_NEVER_NULL);
|
|
|
|
|
RNA_def_property_pointer_sdna(prop, NULL, "curl");
|
|
|
|
|
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
|
|
|
|
|
RNA_def_property_struct_type(prop, "TextLine");
|
|
|
|
|
RNA_def_property_ui_text(
|
|
|
|
|
prop, "Current Line", "Current line, and start line of selection if one exists");
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
|
2019-10-04 06:30:45 +10:00
|
|
|
RNA_def_property_range(prop, 0, INT_MAX);
|
2019-04-17 06:17:24 +02:00
|
|
|
RNA_def_property_ui_text(prop,
|
|
|
|
|
"Current Character",
|
|
|
|
|
"Index of current character in current line, and also start index of "
|
|
|
|
|
"character in selection if one exists");
|
2019-10-04 06:30:45 +10:00
|
|
|
RNA_def_property_int_funcs(
|
|
|
|
|
prop, "rna_Text_current_character_get", "rna_Text_current_character_set", NULL);
|
|
|
|
|
RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "current_line_index", PROP_INT, PROP_NONE);
|
|
|
|
|
RNA_def_property_int_funcs(
|
|
|
|
|
prop, "rna_Text_current_line_index_get", "rna_Text_current_line_index_set", NULL);
|
|
|
|
|
RNA_def_property_ui_text(
|
|
|
|
|
prop, "Current Line Index", "Index of current TextLine in TextLine collection");
|
|
|
|
|
RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
|
|
|
|
|
RNA_def_property_flag(prop, PROP_NEVER_NULL);
|
|
|
|
|
RNA_def_property_pointer_sdna(prop, NULL, "sell");
|
|
|
|
|
RNA_def_property_clear_flag(prop, PROP_EDITABLE);
|
|
|
|
|
RNA_def_property_struct_type(prop, "TextLine");
|
|
|
|
|
RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");
|
|
|
|
|
|
2019-10-04 06:30:45 +10:00
|
|
|
prop = RNA_def_property(srna, "select_end_line_index", PROP_INT, PROP_NONE);
|
|
|
|
|
RNA_def_property_int_funcs(
|
|
|
|
|
prop, "rna_Text_select_end_line_index_get", "rna_Text_select_end_line_index_set", NULL);
|
|
|
|
|
RNA_def_property_ui_text(prop, "Select End Line Index", "Index of last TextLine in selection");
|
|
|
|
|
RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
prop = RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
|
2019-10-04 06:30:45 +10:00
|
|
|
RNA_def_property_range(prop, 0, INT_MAX);
|
2019-04-17 06:17:24 +02:00
|
|
|
RNA_def_property_ui_text(prop,
|
|
|
|
|
"Selection End Character",
|
|
|
|
|
"Index of character after end of selection in the selection end line");
|
2019-10-04 06:30:45 +10:00
|
|
|
RNA_def_property_int_funcs(
|
|
|
|
|
prop, "rna_Text_select_end_character_get", "rna_Text_select_end_character_set", NULL);
|
|
|
|
|
RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
RNA_api_text(srna);
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RNA_def_text(BlenderRNA *brna)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
rna_def_text_line(brna);
|
|
|
|
|
rna_def_text(brna);
|
2008-12-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|