Blender  V2.93
rna_text.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <limits.h>
22 #include <stdlib.h>
23 
24 #include "MEM_guardedalloc.h"
25 
26 #include "BLT_translation.h"
27 
28 #include "BKE_text.h"
29 
30 #include "ED_text.h"
31 
32 #include "RNA_define.h"
33 
34 #include "rna_internal.h"
35 
36 #include "DNA_text_types.h"
37 
38 #include "WM_types.h"
39 
40 #ifdef RNA_RUNTIME
41 
42 static void rna_Text_filename_get(PointerRNA *ptr, char *value)
43 {
44  Text *text = (Text *)ptr->data;
45 
46  if (text->filepath) {
47  strcpy(value, text->filepath);
48  }
49  else {
50  value[0] = '\0';
51  }
52 }
53 
54 static int rna_Text_filename_length(PointerRNA *ptr)
55 {
56  Text *text = (Text *)ptr->data;
57  return (text->filepath) ? strlen(text->filepath) : 0;
58 }
59 
60 static void rna_Text_filename_set(PointerRNA *ptr, const char *value)
61 {
62  Text *text = (Text *)ptr->data;
63 
64  if (text->filepath) {
65  MEM_freeN(text->filepath);
66  }
67 
68  if (value[0]) {
69  text->filepath = BLI_strdup(value);
70  }
71  else {
72  text->filepath = NULL;
73  }
74 }
75 
76 static bool rna_Text_modified_get(PointerRNA *ptr)
77 {
78  Text *text = (Text *)ptr->data;
79  return BKE_text_file_modified_check(text) != 0;
80 }
81 
82 static int rna_Text_current_line_index_get(PointerRNA *ptr)
83 {
84  Text *text = (Text *)ptr->data;
85  return BLI_findindex(&text->lines, text->curl);
86 }
87 
88 static void rna_Text_current_line_index_set(PointerRNA *ptr, int value)
89 {
90  Text *text = ptr->data;
91  TextLine *line = BLI_findlink(&text->lines, value);
92  if (line == NULL) {
93  line = text->lines.last;
94  }
95  text->curl = line;
96  text->curc = 0;
97 }
98 
99 static int rna_Text_select_end_line_index_get(PointerRNA *ptr)
100 {
101  Text *text = ptr->data;
102  return BLI_findindex(&text->lines, text->sell);
103 }
104 
105 static void rna_Text_select_end_line_index_set(PointerRNA *ptr, int value)
106 {
107  Text *text = ptr->data;
108  TextLine *line = BLI_findlink(&text->lines, value);
109  if (line == NULL) {
110  line = text->lines.last;
111  }
112  text->sell = line;
113  text->selc = 0;
114 }
115 
116 static int rna_Text_current_character_get(PointerRNA *ptr)
117 {
118  Text *text = ptr->data;
119  return BLI_str_utf8_offset_to_index(text->curl->line, text->curc);
120 }
121 
122 static void rna_Text_current_character_set(PointerRNA *ptr, int index)
123 {
124  Text *text = ptr->data;
125  TextLine *line = text->curl;
126  const int len_utf8 = BLI_strlen_utf8(line->line);
127  CLAMP_MAX(index, len_utf8);
128  text->curc = BLI_str_utf8_offset_from_index(line->line, index);
129 }
130 
131 static int rna_Text_select_end_character_get(PointerRNA *ptr)
132 {
133  Text *text = ptr->data;
134  return BLI_str_utf8_offset_to_index(text->sell->line, text->selc);
135 }
136 
137 static void rna_Text_select_end_character_set(PointerRNA *ptr, int index)
138 {
139  Text *text = ptr->data;
140  TextLine *line = text->sell;
141  const int len_utf8 = BLI_strlen_utf8(line->line);
142  CLAMP_MAX(index, len_utf8);
143  text->selc = BLI_str_utf8_offset_from_index(line->line, index);
144 }
145 
146 static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
147 {
148  TextLine *line = (TextLine *)ptr->data;
149 
150  if (line->line) {
151  strcpy(value, line->line);
152  }
153  else {
154  value[0] = '\0';
155  }
156 }
157 
158 static int rna_TextLine_body_length(PointerRNA *ptr)
159 {
160  TextLine *line = (TextLine *)ptr->data;
161  return line->len;
162 }
163 
164 static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
165 {
166  TextLine *line = (TextLine *)ptr->data;
167  int len = strlen(value);
168 
169  if (line->line) {
170  MEM_freeN(line->line);
171  }
172 
173  line->line = MEM_mallocN((len + 1) * sizeof(char), "rna_text_body");
174  line->len = len;
175  memcpy(line->line, value, len + 1);
176 
177  if (line->format) {
178  MEM_freeN(line->format);
179  line->format = NULL;
180  }
181 }
182 
183 #else
184 
185 static void rna_def_text_line(BlenderRNA *brna)
186 {
187  StructRNA *srna;
188  PropertyRNA *prop;
189 
190  srna = RNA_def_struct(brna, "TextLine", NULL);
191  RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text data-block");
192 
193  prop = RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
195  prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
196  RNA_def_property_ui_text(prop, "Line", "Text in the line");
199 }
200 
201 static void rna_def_text(BlenderRNA *brna)
202 {
203 
204  static const EnumPropertyItem indentation_items[] = {
205  {0, "TABS", 0, "Tabs", "Indent using tabs"},
206  {TXT_TABSTOSPACES, "SPACES", 0, "Spaces", "Indent using spaces"},
207  {0, NULL, 0, NULL, NULL},
208  };
209 
210  StructRNA *srna;
211  PropertyRNA *prop;
212 
213  srna = RNA_def_struct(brna, "Text", "ID");
215  srna, "Text", "Text data-block referencing an external or packed text file");
216  RNA_def_struct_ui_icon(srna, ICON_TEXT);
218 
219  prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_NONE);
221  prop, "rna_Text_filename_get", "rna_Text_filename_length", "rna_Text_filename_set");
222  RNA_def_property_ui_text(prop, "File Path", "Filename of the text file");
223 
224  prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
227  RNA_def_property_ui_text(prop, "Dirty", "Text file has been edited since last save");
228 
229  prop = RNA_def_property(srna, "is_modified", PROP_BOOLEAN, PROP_NONE);
231  RNA_def_property_boolean_funcs(prop, "rna_Text_modified_get", NULL);
233  prop, "Modified", "Text file on disk is different than the one in memory");
234 
235  prop = RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE);
239  prop, "Memory", "Text file is in memory, without a corresponding file on disk");
240 
241  prop = RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE);
244  prop, "Register", "Run this text as a script on loading, Text name must end with \".py\"");
245 
246  prop = RNA_def_property(srna, "indentation", PROP_ENUM, PROP_NONE); /* as an enum */
248  RNA_def_property_enum_items(prop, indentation_items);
249  RNA_def_property_ui_text(prop, "Indentation", "Use tabs or spaces for indentation");
250 
251  prop = RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
252  RNA_def_property_struct_type(prop, "TextLine");
253  RNA_def_property_ui_text(prop, "Lines", "Lines of text");
254 
255  prop = RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
257  RNA_def_property_pointer_sdna(prop, NULL, "curl");
259  RNA_def_property_struct_type(prop, "TextLine");
261  prop, "Current Line", "Current line, and start line of selection if one exists");
262 
263  prop = RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
264  RNA_def_property_range(prop, 0, INT_MAX);
266  "Current Character",
267  "Index of current character in current line, and also start index of "
268  "character in selection if one exists");
270  prop, "rna_Text_current_character_get", "rna_Text_current_character_set", NULL);
272 
273  prop = RNA_def_property(srna, "current_line_index", PROP_INT, PROP_NONE);
275  prop, "rna_Text_current_line_index_get", "rna_Text_current_line_index_set", NULL);
277  prop, "Current Line Index", "Index of current TextLine in TextLine collection");
279 
280  prop = RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
282  RNA_def_property_pointer_sdna(prop, NULL, "sell");
284  RNA_def_property_struct_type(prop, "TextLine");
285  RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");
286 
287  prop = RNA_def_property(srna, "select_end_line_index", PROP_INT, PROP_NONE);
289  prop, "rna_Text_select_end_line_index_get", "rna_Text_select_end_line_index_set", NULL);
290  RNA_def_property_ui_text(prop, "Select End Line Index", "Index of last TextLine in selection");
292 
293  prop = RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
294  RNA_def_property_range(prop, 0, INT_MAX);
296  "Selection End Character",
297  "Index of character after end of selection in the selection end line");
299  prop, "rna_Text_select_end_character_get", "rna_Text_select_end_character_set", NULL);
301 
302  RNA_api_text(srna);
303 }
304 
306 {
307  rna_def_text_line(brna);
308  rna_def_text(brna);
309 }
310 
311 #endif
int BKE_text_file_modified_check(struct Text *text)
Definition: text.c:557
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:70
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL()
Definition: string_utf8.c:357
size_t int BLI_str_utf8_offset_to_index(const char *str, int offset)
Definition: string_utf8.c:913
int BLI_str_utf8_offset_from_index(const char *str, int index)
Definition: string_utf8.c:923
#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:621
@ PROP_BOOLEAN
Definition: RNA_types.h:73
@ PROP_ENUM
Definition: RNA_types.h:77
@ PROP_INT
Definition: RNA_types.h:74
@ PROP_STRING
Definition: RNA_types.h:76
@ PROP_POINTER
Definition: RNA_types.h:78
@ PROP_COLLECTION
Definition: RNA_types.h:79
@ PROP_EDITABLE
Definition: RNA_types.h:175
@ PROP_NEVER_NULL
Definition: RNA_types.h:225
@ PROP_NONE
Definition: RNA_types.h:113
@ PROP_UNSIGNED
Definition: RNA_types.h:129
#define ND_CURSOR
Definition: WM_types.h:390
#define NA_EDITED
Definition: WM_types.h:462
#define NC_TEXT
Definition: WM_types.h:287
return(oflags[bm->toolflag_index].f &oflag) !=0
size_t len_utf8
Definition: font.c:1758
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2762
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t bit)
Definition: rna_define.c:2257
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
Definition: rna_define.c:3312
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
Definition: rna_define.c:1676
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
Definition: rna_define.c:1259
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
Definition: rna_define.c:2971
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
Definition: rna_define.c:1892
void RNA_def_struct_clear_flag(StructRNA *srna, int flag)
Definition: rna_define.c:1157
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
Definition: rna_define.c:1757
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
Definition: rna_define.c:1792
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
Definition: rna_define.c:2927
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
Definition: rna_define.c:1279
void RNA_def_property_enum_bitflag_sdna(PropertyRNA *prop, const char *structname, const char *propname)
Definition: rna_define.c:2691
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1517
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
Definition: rna_define.c:1047
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
Definition: rna_define.c:3055
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
Definition: rna_define.c:1267
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
Definition: rna_define.c:2870
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1512
void RNA_api_text(struct StructRNA *srna)
Definition: rna_text_api.c:63
static void rna_def_text_line(BlenderRNA *brna)
Definition: rna_text.c:185
static void rna_def_text(BlenderRNA *brna)
Definition: rna_text.c:201
void RNA_def_text(BlenderRNA *brna)
Definition: rna_text.c:305
void * last
Definition: DNA_listBase.h:47
void * data
Definition: RNA_types.h:52
char * format
char * line
ListBase lines
TextLine * curl
int selc
TextLine * sell
int curc
char * filepath
uint len
PointerRNA * ptr
Definition: wm_files.c:3157