Blender  V3.3
text_suggestions.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2008 Blender Foundation. All rights reserved. */
3 
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "MEM_guardedalloc.h"
14 
15 #include "BLI_string.h"
16 
17 #include "BKE_text_suggestions.h" /* Own include. */
18 #include "DNA_text_types.h"
19 
20 /**********************/
21 /* Static definitions */
22 /**********************/
23 
26 static char *documentation = NULL;
27 // static int doc_lines = 0;
28 
29 static void txttl_free_suggest(void)
30 {
31  SuggItem *item, *prev;
32  for (item = suggestions.last; item; item = prev) {
33  prev = item->prev;
34  MEM_freeN(item);
35  }
39  suggestions.top = 0;
40 }
41 
42 static void txttl_free_docs(void)
43 {
45 }
46 
47 /**************************/
48 /* General tool functions */
49 /**************************/
50 
51 void free_texttools(void)
52 {
55 }
56 
58 {
59  if (activeToolText == text) {
60  return;
61  }
63  activeToolText = text;
64 }
65 
67 {
70 }
71 
73 {
74  return activeToolText == text ? 1 : 0;
75 }
76 
77 /***************************/
78 /* Suggestion list methods */
79 /***************************/
80 
81 void texttool_suggest_add(const char *name, char type)
82 {
83  const int len = strlen(name);
84  int cmp;
85  SuggItem *newitem, *item;
86 
87  newitem = MEM_mallocN(sizeof(SuggItem) + len + 1, "SuggItem");
88  if (!newitem) {
89  printf("Failed to allocate memory for suggestion.\n");
90  return;
91  }
92 
93  memcpy(newitem->name, name, len + 1);
94  newitem->type = type;
95  newitem->prev = newitem->next = NULL;
96 
97  /* Perform simple linear search for ordered storage */
98  if (!suggestions.first || !suggestions.last) {
99  suggestions.first = suggestions.last = newitem;
100  }
101  else {
102  cmp = -1;
103  for (item = suggestions.last; item; item = item->prev) {
104  cmp = BLI_strncasecmp(name, item->name, len);
105 
106  /* Newitem comes after this item, insert here */
107  if (cmp >= 0) {
108  newitem->prev = item;
109  if (item->next) {
110  item->next->prev = newitem;
111  }
112  newitem->next = item->next;
113  item->next = newitem;
114 
115  /* At last item, set last pointer here */
116  if (item == suggestions.last) {
117  suggestions.last = newitem;
118  }
119  break;
120  }
121  }
122  /* Reached beginning of list, insert before first */
123  if (cmp < 0) {
124  newitem->next = suggestions.first;
125  suggestions.first->prev = newitem;
126  suggestions.first = newitem;
127  }
128  }
130  suggestions.top = 0;
131 }
132 
133 void texttool_suggest_prefix(const char *prefix, const int prefix_len)
134 {
135  SuggItem *match, *first, *last;
136  int cmp, top = 0;
137 
138  if (!suggestions.first) {
139  return;
140  }
141  if (prefix_len == 0) {
144  return;
145  }
146 
147  first = last = NULL;
148  for (match = suggestions.first; match; match = match->next) {
149  cmp = BLI_strncasecmp(prefix, match->name, prefix_len);
150  if (cmp == 0) {
151  if (!first) {
152  first = match;
153  suggestions.top = top;
154  }
155  }
156  else if (cmp < 0) {
157  if (!last) {
158  last = match->prev;
159  break;
160  }
161  }
162  top++;
163  }
164  if (first) {
165  if (!last) {
166  last = suggestions.last;
167  }
168  suggestions.firstmatch = first;
169  suggestions.lastmatch = last;
170  suggestions.selected = first;
171  }
172  else {
176  suggestions.top = 0;
177  }
178 }
179 
181 {
183 }
184 
186 {
187  return suggestions.firstmatch;
188 }
189 
191 {
192  return suggestions.lastmatch;
193 }
194 
196 {
197  suggestions.selected = sel;
198 }
199 
201 {
202  return suggestions.selected;
203 }
204 
206 {
207  return &suggestions.top;
208 }
209 
210 /*************************/
211 /* Documentation methods */
212 /*************************/
213 
214 void texttool_docs_show(const char *docs)
215 {
216  int len;
217 
218  if (!docs) {
219  return;
220  }
221 
222  len = strlen(docs);
223 
225 
226  /* Ensure documentation ends with a '\n' */
227  if (docs[len - 1] != '\n') {
228  documentation = MEM_mallocN(len + 2, "Documentation");
229  memcpy(documentation, docs, len);
230  documentation[len++] = '\n';
231  }
232  else {
233  documentation = MEM_mallocN(len + 1, "Documentation");
234  memcpy(documentation, docs, len);
235  }
236  documentation[len] = '\0';
237 }
238 
239 char *texttool_docs_get(void)
240 {
241  return documentation;
242 }
243 
245 {
246  txttl_free_docs();
247 }
int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:646
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble top
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
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
SymEdge< T > * prev(const SymEdge< T > *se)
Definition: delaunay_2d.cc:105
struct SuggItem * prev
struct SuggItem * next
SuggItem * first
SuggItem * firstmatch
SuggItem * selected
SuggItem * lastmatch
SuggItem * last
void texttool_docs_show(const char *docs)
void texttool_text_clear(void)
SuggItem * texttool_suggest_last(void)
void texttool_text_set_active(Text *text)
char * texttool_docs_get(void)
static void txttl_free_suggest(void)
void texttool_suggest_select(SuggItem *sel)
void texttool_docs_clear(void)
void texttool_suggest_prefix(const char *prefix, const int prefix_len)
static Text * activeToolText
void texttool_suggest_add(const char *name, char type)
static SuggList suggestions
SuggItem * texttool_suggest_selected(void)
short texttool_text_is_active(Text *text)
void texttool_suggest_clear(void)
SuggItem * texttool_suggest_first(void)
int * texttool_suggest_top(void)
static char * documentation
void free_texttools(void)
static void txttl_free_docs(void)