Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rna_text.c « intern « makesrna « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d2df5b7411ca1229f96e7689b4142ebabb2963d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup RNA
 */

#include <limits.h>
#include <stdlib.h>

#include "MEM_guardedalloc.h"

#include "BLT_translation.h"

#include "BKE_text.h"

#include "ED_text.h"

#include "RNA_define.h"

#include "rna_internal.h"

#include "DNA_text_types.h"

#include "WM_types.h"

#ifdef RNA_RUNTIME

static void rna_Text_filename_get(PointerRNA *ptr, char *value)
{
  Text *text = (Text *)ptr->data;

  if (text->filepath) {
    strcpy(value, text->filepath);
  }
  else {
    value[0] = '\0';
  }
}

static int rna_Text_filename_length(PointerRNA *ptr)
{
  Text *text = (Text *)ptr->data;
  return (text->filepath) ? strlen(text->filepath) : 0;
}

static void rna_Text_filename_set(PointerRNA *ptr, const char *value)
{
  Text *text = (Text *)ptr->data;

  if (text->filepath) {
    MEM_freeN(text->filepath);
  }

  if (value[0]) {
    text->filepath = BLI_strdup(value);
  }
  else {
    text->filepath = NULL;
  }
}

static bool rna_Text_modified_get(PointerRNA *ptr)
{
  Text *text = (Text *)ptr->data;
  return BKE_text_file_modified_check(text) != 0;
}

static int rna_Text_current_line_index_get(PointerRNA *ptr)
{
  Text *text = (Text *)ptr->data;
  return BLI_findindex(&text->lines, text->curl);
}

static void rna_Text_current_line_index_set(PointerRNA *ptr, int value)
{
  Text *text = ptr->data;
  TextLine *line = BLI_findlink(&text->lines, value);
  if (line == NULL) {
    line = text->lines.last;
  }
  text->curl = line;
  text->curc = 0;
}

static int rna_Text_select_end_line_index_get(PointerRNA *ptr)
{
  Text *text = ptr->data;
  return BLI_findindex(&text->lines, text->sell);
}

static void rna_Text_select_end_line_index_set(PointerRNA *ptr, int value)
{
  Text *text = ptr->data;
  TextLine *line = BLI_findlink(&text->lines, value);
  if (line == NULL) {
    line = text->lines.last;
  }
  text->sell = line;
  text->selc = 0;
}

static int rna_Text_current_character_get(PointerRNA *ptr)
{
  Text *text = ptr->data;
  return BLI_str_utf8_offset_to_index(text->curl->line, text->curc);
}

static void rna_Text_current_character_set(PointerRNA *ptr, int index)
{
  Text *text = ptr->data;
  TextLine *line = text->curl;
  const int len_utf8 = BLI_strlen_utf8(line->line);
  CLAMP_MAX(index, len_utf8);
  text->curc = BLI_str_utf8_offset_from_index(line->line, index);
}

static int rna_Text_select_end_character_get(PointerRNA *ptr)
{
  Text *text = ptr->data;
  return BLI_str_utf8_offset_to_index(text->sell->line, text->selc);
}

static void rna_Text_select_end_character_set(PointerRNA *ptr, int index)
{
  Text *text = ptr->data;
  TextLine *line = text->sell;
  const int len_utf8 = BLI_strlen_utf8(line->line);
  CLAMP_MAX(index, len_utf8);
  text->selc = BLI_str_utf8_offset_from_index(line->line, index);
}

static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
{
  TextLine *line = (TextLine *)ptr->data;

  if (line->line) {
    strcpy(value, line->line);
  }
  else {
    value[0] = '\0';
  }
}

static int rna_TextLine_body_length(PointerRNA *ptr)
{
  TextLine *line = (TextLine *)ptr->data;
  return line->len;
}

static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
{
  TextLine *line = (TextLine *)ptr->data;
  int len = strlen(value);

  if (line->line) {
    MEM_freeN(line->line);
  }

  line->line = MEM_mallocN((len + 1) * sizeof(char), "rna_text_body");
  line->len = len;
  memcpy(line->line, value, len + 1);

  if (line->format) {
    MEM_freeN(line->format);
    line->format = NULL;
  }
}

#else

static void rna_def_text_line(BlenderRNA *brna)
{
  StructRNA *srna;
  PropertyRNA *prop;

  srna = RNA_def_struct(brna, "TextLine", NULL);
  RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text data-block");

  prop = RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
  RNA_def_property_string_funcs(
      prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
  RNA_def_property_ui_text(prop, "Line", "Text in the line");
  RNA_def_property_update(prop, NC_TEXT | NA_EDITED, NULL);
  RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_TEXT);
}

static void rna_def_text(BlenderRNA *brna)
{

  static const EnumPropertyItem indentation_items[] = {
      {0, "TABS", 0, "Tabs", "Indent using tabs"},
      {TXT_TABSTOSPACES, "SPACES", 0, "Spaces", "Indent using spaces"},
      {0, NULL, 0, NULL, NULL},
  };

  StructRNA *srna;
  PropertyRNA *prop;

  srna = RNA_def_struct(brna, "Text", "ID");
  RNA_def_struct_ui_text(
      srna, "Text", "Text data-block referencing an external or packed text file");
  RNA_def_struct_ui_icon(srna, ICON_TEXT);
  RNA_def_struct_clear_flag(srna, STRUCT_ID_REFCOUNT);

  prop = RNA_def_property(srna, "filepath", PROP_STRING, PROP_NONE);
  RNA_def_property_string_funcs(
      prop, "rna_Text_filename_get", "rna_Text_filename_length", "rna_Text_filename_set");
  RNA_def_property_ui_text(prop, "File Path", "Filename of the text file");

  prop = RNA_def_property(srna, "is_dirty", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISDIRTY);
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_ui_text(prop, "Dirty", "Text file has been edited since last save");

  prop = RNA_def_property(srna, "is_modified", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_boolean_funcs(prop, "rna_Text_modified_get", NULL);
  RNA_def_property_ui_text(
      prop, "Modified", "Text file on disk is different than the one in memory");

  prop = RNA_def_property(srna, "is_in_memory", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISMEM);
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_ui_text(
      prop, "Memory", "Text file is in memory, without a corresponding file on disk");

  prop = RNA_def_property(srna, "use_module", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flags", TXT_ISSCRIPT);
  RNA_def_property_ui_text(prop, "Register", "Run this text as a Python script on loading");

  prop = RNA_def_property(srna, "indentation", PROP_ENUM, PROP_NONE); /* as an enum */
  RNA_def_property_enum_bitflag_sdna(prop, NULL, "flags");
  RNA_def_property_enum_items(prop, indentation_items);
  RNA_def_property_ui_text(prop, "Indentation", "Use tabs or spaces for indentation");

  prop = RNA_def_property(srna, "lines", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_struct_type(prop, "TextLine");
  RNA_def_property_ui_text(prop, "Lines", "Lines of text");

  prop = RNA_def_property(srna, "current_line", PROP_POINTER, PROP_NONE);
  RNA_def_property_flag(prop, PROP_NEVER_NULL);
  RNA_def_property_pointer_sdna(prop, NULL, "curl");
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_struct_type(prop, "TextLine");
  RNA_def_property_ui_text(
      prop, "Current Line", "Current line, and start line of selection if one exists");

  prop = RNA_def_property(srna, "current_character", PROP_INT, PROP_UNSIGNED);
  RNA_def_property_range(prop, 0, INT_MAX);
  RNA_def_property_ui_text(prop,
                           "Current Character",
                           "Index of current character in current line, and also start index of "
                           "character in selection if one exists");
  RNA_def_property_int_funcs(
      prop, "rna_Text_current_character_get", "rna_Text_current_character_set", NULL);
  RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);

  prop = RNA_def_property(srna, "current_line_index", PROP_INT, PROP_NONE);
  RNA_def_property_int_funcs(
      prop, "rna_Text_current_line_index_get", "rna_Text_current_line_index_set", NULL);
  RNA_def_property_ui_text(
      prop, "Current Line Index", "Index of current TextLine in TextLine collection");
  RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);

  prop = RNA_def_property(srna, "select_end_line", PROP_POINTER, PROP_NONE);
  RNA_def_property_flag(prop, PROP_NEVER_NULL);
  RNA_def_property_pointer_sdna(prop, NULL, "sell");
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_struct_type(prop, "TextLine");
  RNA_def_property_ui_text(prop, "Selection End Line", "End line of selection");

  prop = RNA_def_property(srna, "select_end_line_index", PROP_INT, PROP_NONE);
  RNA_def_property_int_funcs(
      prop, "rna_Text_select_end_line_index_get", "rna_Text_select_end_line_index_set", NULL);
  RNA_def_property_ui_text(prop, "Select End Line Index", "Index of last TextLine in selection");
  RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);

  prop = RNA_def_property(srna, "select_end_character", PROP_INT, PROP_UNSIGNED);
  RNA_def_property_range(prop, 0, INT_MAX);
  RNA_def_property_ui_text(prop,
                           "Selection End Character",
                           "Index of character after end of selection in the selection end line");
  RNA_def_property_int_funcs(
      prop, "rna_Text_select_end_character_get", "rna_Text_select_end_character_set", NULL);
  RNA_def_property_update(prop, NC_TEXT | ND_CURSOR, NULL);

  RNA_api_text(srna);
}

void RNA_def_text(BlenderRNA *brna)
{
  rna_def_text_line(brna);
  rna_def_text(brna);
}

#endif