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