Blender V4.3
rna_text.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <climits>
10#include <cstdlib>
11
12#include "MEM_guardedalloc.h"
13
14#include "BLT_translation.hh"
15
16#include "BKE_text.h"
17
18#include "ED_text.hh"
19
20#include "RNA_define.hh"
21
22#include "rna_internal.hh"
23
24#include "DNA_text_types.h"
25
26#include "WM_types.hh"
27
28#ifdef RNA_RUNTIME
29
30static void rna_Text_filepath_get(PointerRNA *ptr, char *value)
31{
32 const Text *text = (const Text *)ptr->data;
33
34 if (text->filepath) {
35 strcpy(value, text->filepath);
36 }
37 else {
38 value[0] = '\0';
39 }
40}
41
42static int rna_Text_filepath_length(PointerRNA *ptr)
43{
44 const Text *text = (const Text *)ptr->data;
45 return (text->filepath) ? strlen(text->filepath) : 0;
46}
47
48static void rna_Text_filepath_set(PointerRNA *ptr, const char *value)
49{
50 Text *text = (Text *)ptr->data;
51
52 if (text->filepath) {
53 MEM_freeN(text->filepath);
54 }
55
56 if (value[0]) {
57 text->filepath = BLI_strdup(value);
58 }
59 else {
60 text->filepath = nullptr;
61 }
62}
63
64static bool rna_Text_modified_get(PointerRNA *ptr)
65{
66 const Text *text = (const Text *)ptr->data;
67 return BKE_text_file_modified_check(text) != 0;
68}
69
70static int rna_Text_current_line_index_get(PointerRNA *ptr)
71{
72 const Text *text = (const Text *)ptr->data;
73 return BLI_findindex(&text->lines, text->curl);
74}
75
76static void rna_Text_current_line_index_set(PointerRNA *ptr, int value)
77{
78 Text *text = static_cast<Text *>(ptr->data);
79 TextLine *line = static_cast<TextLine *>(BLI_findlink(&text->lines, value));
80 if (line == nullptr) {
81 line = static_cast<TextLine *>(text->lines.last);
82 }
83 text->curl = line;
84 text->curc = 0;
85}
86
87static int rna_Text_select_end_line_index_get(PointerRNA *ptr)
88{
89 const Text *text = static_cast<Text *>(ptr->data);
90 return BLI_findindex(&text->lines, text->sell);
91}
92
93static void rna_Text_select_end_line_index_set(PointerRNA *ptr, int value)
94{
95 Text *text = static_cast<Text *>(ptr->data);
96 TextLine *line = static_cast<TextLine *>(BLI_findlink(&text->lines, value));
97 if (line == nullptr) {
98 line = static_cast<TextLine *>(text->lines.last);
99 }
100 text->sell = line;
101 text->selc = 0;
102}
103
104static int rna_Text_current_character_get(PointerRNA *ptr)
105{
106 const Text *text = static_cast<const Text *>(ptr->data);
107 const TextLine *line = text->curl;
108 return BLI_str_utf8_offset_to_index(line->line, line->len, text->curc);
109}
110
111static void rna_Text_current_character_set(PointerRNA *ptr, const int index)
112{
113 Text *text = static_cast<Text *>(ptr->data);
114 TextLine *line = text->curl;
115 text->curc = BLI_str_utf8_offset_from_index(line->line, line->len, index);
116}
117
118static int rna_Text_select_end_character_get(PointerRNA *ptr)
119{
120 Text *text = static_cast<Text *>(ptr->data);
121 TextLine *line = text->sell;
122 return BLI_str_utf8_offset_to_index(line->line, line->len, text->selc);
123}
124
125static void rna_Text_select_end_character_set(PointerRNA *ptr, const int index)
126{
127 Text *text = static_cast<Text *>(ptr->data);
128 TextLine *line = text->sell;
129 text->selc = BLI_str_utf8_offset_from_index(line->line, line->len, index);
130}
131
132static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
133{
134 const TextLine *line = (const TextLine *)ptr->data;
135
136 if (line->line) {
137 strcpy(value, line->line);
138 }
139 else {
140 value[0] = '\0';
141 }
142}
143
144static int rna_TextLine_body_length(PointerRNA *ptr)
145{
146 const TextLine *line = (const TextLine *)ptr->data;
147 return line->len;
148}
149
150static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
151{
152 TextLine *line = (TextLine *)ptr->data;
153 int len = strlen(value);
154
155 if (line->line) {
156 MEM_freeN(line->line);
157 }
158
159 line->line = static_cast<char *>(MEM_mallocN((len + 1) * sizeof(char), "rna_text_body"));
160 line->len = len;
161 memcpy(line->line, value, len + 1);
162
163 if (line->format) {
164 MEM_freeN(line->format);
165 line->format = nullptr;
166 }
167}
168
169#else
170
172{
173 StructRNA *srna;
174 PropertyRNA *prop;
175
176 srna = RNA_def_struct(brna, "TextLine", nullptr);
177 RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text data-block");
178
179 prop = RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
181 prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
182 RNA_def_property_ui_text(prop, "Line", "Text in the line");
183 RNA_def_property_update(prop, NC_TEXT | NA_EDITED, nullptr);
185}
186
187static void rna_def_text(BlenderRNA *brna)
188{
189
190 static const EnumPropertyItem indentation_items[] = {
191 {0, "TABS", 0, "Tabs", "Indent using tabs"},
192 {TXT_TABSTOSPACES, "SPACES", 0, "Spaces", "Indent using spaces"},
193 {0, nullptr, 0, nullptr, nullptr},
194 };
195
196 StructRNA *srna;
197 PropertyRNA *prop;
198
199 srna = RNA_def_struct(brna, "Text", "ID");
201 srna, "Text", "Text data-block referencing an external or packed text file");
202 RNA_def_struct_ui_icon(srna, ICON_TEXT);
204
205 prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_NONE);
207 prop, "rna_Text_filepath_get", "rna_Text_filepath_length", "rna_Text_filepath_set");
208 RNA_def_property_ui_text(prop, "File Path", "Filename of the text file");
209
210 prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
211 RNA_def_property_boolean_sdna(prop, nullptr, "flags", TXT_ISDIRTY);
213 RNA_def_property_ui_text(prop, "Dirty", "Text file has been edited since last save");
214
215 prop = RNA_def_property(srna, "is_modified", PROP_BOOLEAN, PROP_NONE);
217 RNA_def_property_boolean_funcs(prop, "rna_Text_modified_get", nullptr);
220 prop, "Modified", "Text file on disk is different than the one in memory");
221
222 prop = RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE);
223 RNA_def_property_boolean_sdna(prop, nullptr, "flags", TXT_ISMEM);
226 prop, "Memory", "Text file is in memory, without a corresponding file on disk");
227
228 prop = RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE);
229 RNA_def_property_boolean_sdna(prop, nullptr, "flags", TXT_ISSCRIPT);
230 RNA_def_property_ui_text(prop, "Register", "Run this text as a Python script on loading");
231
232 prop = RNA_def_property(srna, "indentation", PROP_ENUM, PROP_NONE); /* as an enum */
233 RNA_def_property_enum_bitflag_sdna(prop, nullptr, "flags");
234 RNA_def_property_enum_items(prop, indentation_items);
236 RNA_def_property_ui_text(prop, "Indentation", "Use tabs or spaces for indentation");
237
238 prop = RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
239 RNA_def_property_struct_type(prop, "TextLine");
240 RNA_def_property_ui_text(prop, "Lines", "Lines of text");
242
243 prop = RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
245 RNA_def_property_pointer_sdna(prop, nullptr, "curl");
247 RNA_def_property_struct_type(prop, "TextLine");
249 prop, "Current Line", "Current line, and start line of selection if one exists");
250
251 prop = RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
252 RNA_def_property_range(prop, 0, INT_MAX);
254 "Current Character",
255 "Index of current character in current line, and also start index of "
256 "character in selection if one exists");
258 prop, "rna_Text_current_character_get", "rna_Text_current_character_set", nullptr);
259 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
260
261 prop = RNA_def_property(srna, "current_line_index", PROP_INT, PROP_NONE);
263 prop, "rna_Text_current_line_index_get", "rna_Text_current_line_index_set", nullptr);
265 prop, "Current Line Index", "Index of current TextLine in TextLine collection");
266 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
267
268 prop = RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
270 RNA_def_property_pointer_sdna(prop, nullptr, "sell");
272 RNA_def_property_struct_type(prop, "TextLine");
273 RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");
274
275 prop = RNA_def_property(srna, "select_end_line_index", PROP_INT, PROP_NONE);
277 prop, "rna_Text_select_end_line_index_get", "rna_Text_select_end_line_index_set", nullptr);
278 RNA_def_property_ui_text(prop, "Select End Line Index", "Index of last TextLine in selection");
279 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
280
281 prop = RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
282 RNA_def_property_range(prop, 0, INT_MAX);
284 "Selection End Character",
285 "Index of character after end of selection in the selection end line");
287 prop, "rna_Text_select_end_character_get", "rna_Text_select_end_character_set", nullptr);
288 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
289
290 RNA_api_text(srna);
291}
292
294{
295 rna_def_text_line(brna);
296 rna_def_text(brna);
297}
298
299#endif
void int BKE_text_file_modified_check(const struct Text *text)
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.c:40
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
size_t size_t size_t int BLI_str_utf8_offset_to_index(const char *str, size_t str_len, int offset_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define BLT_I18NCONTEXT_ID_TEXT
@ TXT_TABSTOSPACES
@ TXT_ISDIRTY
@ TXT_ISSCRIPT
@ TXT_ISMEM
Read Guarded memory(de)allocation.
@ STRUCT_ID_REFCOUNT
Definition RNA_types.hh:720
@ PROP_BOOLEAN
Definition RNA_types.hh:65
@ PROP_ENUM
Definition RNA_types.hh:69
@ PROP_INT
Definition RNA_types.hh:66
@ PROP_STRING
Definition RNA_types.hh:68
@ PROP_POINTER
Definition RNA_types.hh:70
@ PROP_COLLECTION
Definition RNA_types.hh:71
@ PROP_EDITABLE
Definition RNA_types.hh:207
@ PROP_NEVER_NULL
Definition RNA_types.hh:266
@ PROP_NONE
Definition RNA_types.hh:136
@ PROP_UNSIGNED
Definition RNA_types.hh:152
#define ND_CURSOR
Definition WM_types.hh:457
#define NA_EDITED
Definition WM_types.hh:550
#define NC_TEXT
Definition WM_types.hh:353
int len
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t bit)
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
void RNA_def_struct_clear_flag(StructRNA *srna, int flag)
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
void RNA_def_property_enum_bitflag_sdna(PropertyRNA *prop, const char *structname, const char *propname)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_api_text(StructRNA *srna)
static void rna_def_text_line(BlenderRNA *brna)
Definition rna_text.cc:171
static void rna_def_text(BlenderRNA *brna)
Definition rna_text.cc:187
void RNA_def_text(BlenderRNA *brna)
Definition rna_text.cc:293
void * data
Definition RNA_types.hh:42
PointerRNA * ptr
Definition wm_files.cc:4126