Blender  V3.3
rna_text.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <limits.h>
8 #include <stdlib.h>
9 
10 #include "MEM_guardedalloc.h"
11 
12 #include "BLT_translation.h"
13 
14 #include "BKE_text.h"
15 
16 #include "ED_text.h"
17 
18 #include "RNA_define.h"
19 
20 #include "rna_internal.h"
21 
22 #include "DNA_text_types.h"
23 
24 #include "WM_types.h"
25 
26 #ifdef RNA_RUNTIME
27 
28 static void rna_Text_filepath_get(PointerRNA *ptr, char *value)
29 {
30  Text *text = (Text *)ptr->data;
31 
32  if (text->filepath) {
33  strcpy(value, text->filepath);
34  }
35  else {
36  value[0] = '\0';
37  }
38 }
39 
40 static int rna_Text_filepath_length(PointerRNA *ptr)
41 {
42  Text *text = (Text *)ptr->data;
43  return (text->filepath) ? strlen(text->filepath) : 0;
44 }
45 
46 static 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 = NULL;
59  }
60 }
61 
62 static bool rna_Text_modified_get(PointerRNA *ptr)
63 {
64  Text *text = (Text *)ptr->data;
65  return BKE_text_file_modified_check(text) != 0;
66 }
67 
68 static int rna_Text_current_line_index_get(PointerRNA *ptr)
69 {
70  Text *text = (Text *)ptr->data;
71  return BLI_findindex(&text->lines, text->curl);
72 }
73 
74 static void rna_Text_current_line_index_set(PointerRNA *ptr, int value)
75 {
76  Text *text = ptr->data;
77  TextLine *line = BLI_findlink(&text->lines, value);
78  if (line == NULL) {
79  line = text->lines.last;
80  }
81  text->curl = line;
82  text->curc = 0;
83 }
84 
85 static int rna_Text_select_end_line_index_get(PointerRNA *ptr)
86 {
87  Text *text = ptr->data;
88  return BLI_findindex(&text->lines, text->sell);
89 }
90 
91 static void rna_Text_select_end_line_index_set(PointerRNA *ptr, int value)
92 {
93  Text *text = ptr->data;
94  TextLine *line = BLI_findlink(&text->lines, value);
95  if (line == NULL) {
96  line = text->lines.last;
97  }
98  text->sell = line;
99  text->selc = 0;
100 }
101 
102 static int rna_Text_current_character_get(PointerRNA *ptr)
103 {
104  Text *text = ptr->data;
105  return BLI_str_utf8_offset_to_index(text->curl->line, text->curc);
106 }
107 
108 static void rna_Text_current_character_set(PointerRNA *ptr, int index)
109 {
110  Text *text = ptr->data;
111  TextLine *line = text->curl;
112  const int len_utf8 = BLI_strlen_utf8(line->line);
113  CLAMP_MAX(index, len_utf8);
114  text->curc = BLI_str_utf8_offset_from_index(line->line, index);
115 }
116 
117 static int rna_Text_select_end_character_get(PointerRNA *ptr)
118 {
119  Text *text = ptr->data;
120  return BLI_str_utf8_offset_to_index(text->sell->line, text->selc);
121 }
122 
123 static void rna_Text_select_end_character_set(PointerRNA *ptr, int index)
124 {
125  Text *text = ptr->data;
126  TextLine *line = text->sell;
127  const int len_utf8 = BLI_strlen_utf8(line->line);
128  CLAMP_MAX(index, len_utf8);
129  text->selc = BLI_str_utf8_offset_from_index(line->line, index);
130 }
131 
132 static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
133 {
134  TextLine *line = (TextLine *)ptr->data;
135 
136  if (line->line) {
137  strcpy(value, line->line);
138  }
139  else {
140  value[0] = '\0';
141  }
142 }
143 
144 static int rna_TextLine_body_length(PointerRNA *ptr)
145 {
146  TextLine *line = (TextLine *)ptr->data;
147  return line->len;
148 }
149 
150 static 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 = 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 = NULL;
166  }
167 }
168 
169 #else
170 
171 static void rna_def_text_line(BlenderRNA *brna)
172 {
173  StructRNA *srna;
174  PropertyRNA *prop;
175 
176  srna = RNA_def_struct(brna, "TextLine", NULL);
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");
185 }
186 
187 static 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, NULL, 0, NULL, NULL},
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);
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", NULL);
219  prop, "Modified", "Text file on disk is different than the one in memory");
220 
221  prop = RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE);
225  prop, "Memory", "Text file is in memory, without a corresponding file on disk");
226 
227  prop = RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE);
229  RNA_def_property_ui_text(prop, "Register", "Run this text as a Python script on loading");
230 
231  prop = RNA_def_property(srna, "indentation", PROP_ENUM, PROP_NONE); /* as an enum */
233  RNA_def_property_enum_items(prop, indentation_items);
234  RNA_def_property_ui_text(prop, "Indentation", "Use tabs or spaces for indentation");
235 
236  prop = RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
237  RNA_def_property_struct_type(prop, "TextLine");
238  RNA_def_property_ui_text(prop, "Lines", "Lines of text");
239 
240  prop = RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
242  RNA_def_property_pointer_sdna(prop, NULL, "curl");
244  RNA_def_property_struct_type(prop, "TextLine");
246  prop, "Current Line", "Current line, and start line of selection if one exists");
247 
248  prop = RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
249  RNA_def_property_range(prop, 0, INT_MAX);
251  "Current Character",
252  "Index of current character in current line, and also start index of "
253  "character in selection if one exists");
255  prop, "rna_Text_current_character_get", "rna_Text_current_character_set", NULL);
257 
258  prop = RNA_def_property(srna, "current_line_index", PROP_INT, PROP_NONE);
260  prop, "rna_Text_current_line_index_get", "rna_Text_current_line_index_set", NULL);
262  prop, "Current Line Index", "Index of current TextLine in TextLine collection");
264 
265  prop = RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
267  RNA_def_property_pointer_sdna(prop, NULL, "sell");
269  RNA_def_property_struct_type(prop, "TextLine");
270  RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");
271 
272  prop = RNA_def_property(srna, "select_end_line_index", PROP_INT, PROP_NONE);
274  prop, "rna_Text_select_end_line_index_get", "rna_Text_select_end_line_index_set", NULL);
275  RNA_def_property_ui_text(prop, "Select End Line Index", "Index of last TextLine in selection");
277 
278  prop = RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
279  RNA_def_property_range(prop, 0, INT_MAX);
281  "Selection End Character",
282  "Index of character after end of selection in the selection end line");
284  prop, "rna_Text_select_end_character_get", "rna_Text_select_end_character_set", NULL);
286 
287  RNA_api_text(srna);
288 }
289 
291 {
292  rna_def_text_line(brna);
293  rna_def_text(brna);
294 }
295 
296 #endif
void int BKE_text_file_modified_check(struct Text *text)
Definition: text.c:530
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
int BLI_str_utf8_offset_from_index(const char *str, int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: string_utf8.c:771
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
Definition: string_utf8.c:317
size_t size_t size_t int BLI_str_utf8_offset_to_index(const char *str, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: string_utf8.c:761
#define CLAMP_MAX(a, c)
#define BLT_I18NCONTEXT_ID_TEXT
@ TXT_TABSTOSPACES
@ TXT_ISDIRTY
@ TXT_ISSCRIPT
@ TXT_ISMEM
Read Guarded memory(de)allocation.
@ STRUCT_ID_REFCOUNT
Definition: RNA_types.h:706
@ PROP_BOOLEAN
Definition: RNA_types.h:59
@ PROP_ENUM
Definition: RNA_types.h:63
@ PROP_INT
Definition: RNA_types.h:60
@ PROP_STRING
Definition: RNA_types.h:62
@ PROP_POINTER
Definition: RNA_types.h:64
@ PROP_COLLECTION
Definition: RNA_types.h:65
@ PROP_EDITABLE
Definition: RNA_types.h:189
@ PROP_NEVER_NULL
Definition: RNA_types.h:239
@ PROP_NONE
Definition: RNA_types.h:126
@ PROP_UNSIGNED
Definition: RNA_types.h:142
#define ND_CURSOR
Definition: WM_types.h:438
#define NA_EDITED
Definition: WM_types.h:523
#define NC_TEXT
Definition: WM_types.h:336
return(oflags[bm->toolflag_index].f &oflag) !=0
int len
Definition: draw_manager.c:108
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2740
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t bit)
Definition: rna_define.c:2236
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
Definition: rna_define.c:3285
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1645
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
Definition: rna_define.c:1237
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
Definition: rna_define.c:2944
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
Definition: rna_define.c:1872
void RNA_def_struct_clear_flag(StructRNA *srna, int flag)
Definition: rna_define.c:1138
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
Definition: rna_define.c:1737
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
Definition: rna_define.c:1772
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
Definition: rna_define.c:2900
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1257
void RNA_def_property_enum_bitflag_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2669
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1495
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1028
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3028
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
Definition: rna_define.c:1245
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
Definition: rna_define.c:2848
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1490
void RNA_api_text(struct StructRNA *srna)
Definition: rna_text_api.c:62
static void rna_def_text_line(BlenderRNA *brna)
Definition: rna_text.c:171
static void rna_def_text(BlenderRNA *brna)
Definition: rna_text.c:187
void RNA_def_text(BlenderRNA *brna)
Definition: rna_text.c:290
void * last
Definition: DNA_listBase.h:31
void * data
Definition: RNA_types.h:38
char * format
char * line
ListBase lines
TextLine * curl
int selc
TextLine * sell
int curc
char * filepath
size_t len_utf8
Definition: vfont.c:1759
PointerRNA * ptr
Definition: wm_files.c:3480