Blender V5.0
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 "BKE_text.h"
25
26# include "ED_text.hh"
27
28static void rna_Text_filepath_get(PointerRNA *ptr, char *value)
29{
30 const Text *text = (const Text *)ptr->data;
31
32 if (text->filepath) {
33 strcpy(value, text->filepath);
34 }
35 else {
36 value[0] = '\0';
37 }
38}
39
40static int rna_Text_filepath_length(PointerRNA *ptr)
41{
42 const Text *text = (const Text *)ptr->data;
43 return (text->filepath) ? strlen(text->filepath) : 0;
44}
45
46static void rna_Text_filepath_set(PointerRNA *ptr, const char *value)
47{
48 Text *text = (Text *)ptr->data;
49
50 if (text->filepath) {
51 MEM_freeN(text->filepath);
52 }
53
54 if (value[0]) {
55 text->filepath = BLI_strdup(value);
56 }
57 else {
58 text->filepath = nullptr;
59 }
60}
61
62static bool rna_Text_modified_get(PointerRNA *ptr)
63{
64 const Text *text = (const Text *)ptr->data;
65 return BKE_text_file_modified_check(text) != 0;
66}
67
68static int rna_Text_current_line_index_get(PointerRNA *ptr)
69{
70 const Text *text = (const Text *)ptr->data;
71 return BLI_findindex(&text->lines, text->curl);
72}
73
74static void rna_Text_current_line_index_set(PointerRNA *ptr, int value)
75{
76 Text *text = static_cast<Text *>(ptr->data);
77 TextLine *line = static_cast<TextLine *>(BLI_findlink(&text->lines, value));
78 if (line == nullptr) {
79 line = static_cast<TextLine *>(text->lines.last);
80 }
81 text->curl = line;
82 text->curc = 0;
83}
84
85static int rna_Text_select_end_line_index_get(PointerRNA *ptr)
86{
87 const Text *text = static_cast<Text *>(ptr->data);
88 return BLI_findindex(&text->lines, text->sell);
89}
90
91static void rna_Text_select_end_line_index_set(PointerRNA *ptr, int value)
92{
93 Text *text = static_cast<Text *>(ptr->data);
94 TextLine *line = static_cast<TextLine *>(BLI_findlink(&text->lines, value));
95 if (line == nullptr) {
96 line = static_cast<TextLine *>(text->lines.last);
97 }
98 text->sell = line;
99 text->selc = 0;
100}
101
102static int rna_Text_current_character_get(PointerRNA *ptr)
103{
104 const Text *text = static_cast<const Text *>(ptr->data);
105 const TextLine *line = text->curl;
106 return BLI_str_utf8_offset_to_index(line->line, line->len, text->curc);
107}
108
109static void rna_Text_current_character_set(PointerRNA *ptr, const int index)
110{
111 Text *text = static_cast<Text *>(ptr->data);
112 TextLine *line = text->curl;
113 text->curc = BLI_str_utf8_offset_from_index(line->line, line->len, index);
114}
115
116static int rna_Text_select_end_character_get(PointerRNA *ptr)
117{
118 Text *text = static_cast<Text *>(ptr->data);
119 TextLine *line = text->sell;
120 return BLI_str_utf8_offset_to_index(line->line, line->len, text->selc);
121}
122
123static void rna_Text_select_end_character_set(PointerRNA *ptr, const int index)
124{
125 Text *text = static_cast<Text *>(ptr->data);
126 TextLine *line = text->sell;
127 text->selc = BLI_str_utf8_offset_from_index(line->line, line->len, index);
128}
129
130static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
131{
132 const TextLine *line = (const TextLine *)ptr->data;
133
134 if (line->line) {
135 strcpy(value, line->line);
136 }
137 else {
138 value[0] = '\0';
139 }
140}
141
142static int rna_TextLine_body_length(PointerRNA *ptr)
143{
144 const TextLine *line = (const TextLine *)ptr->data;
145 return line->len;
146}
147
148static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
149{
150 TextLine *line = (TextLine *)ptr->data;
151 size_t len = strlen(value);
152
153 if (line->line) {
154 MEM_freeN(line->line);
155 }
156
157 line->line = MEM_malloc_arrayN<char>(len + 1, "rna_text_body");
158 line->len = int(len);
159 memcpy(line->line, value, len + 1);
160
161 MEM_SAFE_FREE(line->format);
162}
163
164#else
165
167{
168 StructRNA *srna;
169 PropertyRNA *prop;
170
171 srna = RNA_def_struct(brna, "TextLine", nullptr);
172 RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text data-block");
173
174 prop = RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
176 prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
177 RNA_def_property_ui_text(prop, "Line", "Text in the line");
178 RNA_def_property_update(prop, NC_TEXT | NA_EDITED, nullptr);
180}
181
182static void rna_def_text(BlenderRNA *brna)
183{
184
185 static const EnumPropertyItem indentation_items[] = {
186 {0, "TABS", 0, "Tabs", "Indent using tabs"},
187 {TXT_TABSTOSPACES, "SPACES", 0, "Spaces", "Indent using spaces"},
188 {0, nullptr, 0, nullptr, nullptr},
189 };
190
191 StructRNA *srna;
192 PropertyRNA *prop;
193
194 srna = RNA_def_struct(brna, "Text", "ID");
196 srna, "Text", "Text data-block referencing an external or packed text file");
197 RNA_def_struct_ui_icon(srna, ICON_TEXT);
199
200 prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_NONE);
202 prop, "rna_Text_filepath_get", "rna_Text_filepath_length", "rna_Text_filepath_set");
203 RNA_def_property_ui_text(prop, "File Path", "Filename of the text file");
204
205 prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
206 RNA_def_property_boolean_sdna(prop, nullptr, "flags", TXT_ISDIRTY);
208 RNA_def_property_ui_text(prop, "Dirty", "Text file has been edited since last save");
209
210 prop = RNA_def_property(srna, "is_modified", PROP_BOOLEAN, PROP_NONE);
212 RNA_def_property_boolean_funcs(prop, "rna_Text_modified_get", nullptr);
215 prop, "Modified", "Text file on disk is different than the one in memory");
216
217 prop = RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE);
218 RNA_def_property_boolean_sdna(prop, nullptr, "flags", TXT_ISMEM);
221 prop, "Memory", "Text file is in memory, without a corresponding file on disk");
222
223 prop = RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE);
224 RNA_def_property_boolean_sdna(prop, nullptr, "flags", TXT_ISSCRIPT);
225 RNA_def_property_ui_text(prop, "Register", "Run this text as a Python script on loading");
226
227 prop = RNA_def_property(srna, "indentation", PROP_ENUM, PROP_NONE); /* as an enum */
228 RNA_def_property_enum_bitflag_sdna(prop, nullptr, "flags");
229 RNA_def_property_enum_items(prop, indentation_items);
231 RNA_def_property_ui_text(prop, "Indentation", "Use tabs or spaces for indentation");
232
233 prop = RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
234 RNA_def_property_struct_type(prop, "TextLine");
235 RNA_def_property_ui_text(prop, "Lines", "Lines of text");
237
238 prop = RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
240 RNA_def_property_pointer_sdna(prop, nullptr, "curl");
242 RNA_def_property_struct_type(prop, "TextLine");
244 prop, "Current Line", "Current line, and start line of selection if one exists");
245
246 prop = RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
247 RNA_def_property_range(prop, 0, INT_MAX);
249 "Current Character",
250 "Index of current character in current line, and also start index of "
251 "character in selection if one exists");
253 prop, "rna_Text_current_character_get", "rna_Text_current_character_set", nullptr);
254 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
255
256 prop = RNA_def_property(srna, "current_line_index", PROP_INT, PROP_NONE);
258 prop, "rna_Text_current_line_index_get", "rna_Text_current_line_index_set", nullptr);
260 prop, "Current Line Index", "Index of current TextLine in TextLine collection");
261 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
262
263 prop = RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
265 RNA_def_property_pointer_sdna(prop, nullptr, "sell");
267 RNA_def_property_struct_type(prop, "TextLine");
268 RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");
269
270 prop = RNA_def_property(srna, "select_end_line_index", PROP_INT, PROP_NONE);
272 prop, "rna_Text_select_end_line_index_get", "rna_Text_select_end_line_index_set", nullptr);
273 RNA_def_property_ui_text(prop, "Select End Line Index", "Index of last TextLine in selection");
274 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
275
276 prop = RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
277 RNA_def_property_range(prop, 0, INT_MAX);
279 "Selection End Character",
280 "Index of character after end of selection in the selection end line");
282 prop, "rna_Text_select_end_character_get", "rna_Text_select_end_character_set", nullptr);
283 RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, nullptr);
284
285 RNA_api_text(srna);
286}
287
289{
290 rna_def_text_line(brna);
291 rna_def_text(brna);
292}
293
294#endif
void int BKE_text_file_modified_check(const struct Text *text)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:534
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
Definition string.cc:41
int BLI_str_utf8_offset_from_index(const char *str, size_t str_len, int index_target) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
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
#define MEM_SAFE_FREE(v)
@ STRUCT_ID_REFCOUNT
Definition RNA_types.hh:959
@ PROP_BOOLEAN
Definition RNA_types.hh:162
@ PROP_ENUM
Definition RNA_types.hh:166
@ PROP_INT
Definition RNA_types.hh:163
@ PROP_STRING
Definition RNA_types.hh:165
@ PROP_POINTER
Definition RNA_types.hh:167
@ PROP_COLLECTION
Definition RNA_types.hh:168
@ PROP_EDITABLE
Definition RNA_types.hh:306
@ PROP_NEVER_NULL
Definition RNA_types.hh:377
@ PROP_NONE
Definition RNA_types.hh:233
@ PROP_UNSIGNED
Definition RNA_types.hh:249
#define ND_CURSOR
Definition WM_types.hh:490
#define NA_EDITED
Definition WM_types.hh:584
#define NC_TEXT
Definition WM_types.hh:386
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t booleanbit)
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
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:166
static void rna_def_text(BlenderRNA *brna)
Definition rna_text.cc:182
void RNA_def_text(BlenderRNA *brna)
Definition rna_text.cc:288
void * last
char * format
char * line
ListBase lines
TextLine * curl
TextLine * sell
uint len
PointerRNA * ptr
Definition wm_files.cc:4238