Blender  V2.93
text_suggestions.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  * The Original Code is Copyright (C) 2008, Blender Foundation
17  * All rights reserved.
18  */
19 
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "BLI_string.h"
32 
33 #include "BKE_text_suggestions.h" /* Own include. */
34 #include "DNA_text_types.h"
35 
36 /**********************/
37 /* Static definitions */
38 /**********************/
39 
42 static char *documentation = NULL;
43 // static int doc_lines = 0;
44 
45 static void txttl_free_suggest(void)
46 {
47  SuggItem *item, *prev;
48  for (item = suggestions.last; item; item = prev) {
49  prev = item->prev;
50  MEM_freeN(item);
51  }
55  suggestions.top = 0;
56 }
57 
58 static void txttl_free_docs(void)
59 {
60  if (documentation) {
63  }
64 }
65 
66 /**************************/
67 /* General tool functions */
68 /**************************/
69 
70 void free_texttools(void)
71 {
74 }
75 
77 {
78  if (activeToolText == text) {
79  return;
80  }
82  activeToolText = text;
83 }
84 
86 {
89 }
90 
92 {
93  return activeToolText == text ? 1 : 0;
94 }
95 
96 /***************************/
97 /* Suggestion list methods */
98 /***************************/
99 
100 void texttool_suggest_add(const char *name, char type)
101 {
102  const int len = strlen(name);
103  int cmp;
104  SuggItem *newitem, *item;
105 
106  newitem = MEM_mallocN(sizeof(SuggItem) + len + 1, "SuggItem");
107  if (!newitem) {
108  printf("Failed to allocate memory for suggestion.\n");
109  return;
110  }
111 
112  memcpy(newitem->name, name, len + 1);
113  newitem->type = type;
114  newitem->prev = newitem->next = NULL;
115 
116  /* Perform simple linear search for ordered storage */
117  if (!suggestions.first || !suggestions.last) {
118  suggestions.first = suggestions.last = newitem;
119  }
120  else {
121  cmp = -1;
122  for (item = suggestions.last; item; item = item->prev) {
123  cmp = BLI_strncasecmp(name, item->name, len);
124 
125  /* Newitem comes after this item, insert here */
126  if (cmp >= 0) {
127  newitem->prev = item;
128  if (item->next) {
129  item->next->prev = newitem;
130  }
131  newitem->next = item->next;
132  item->next = newitem;
133 
134  /* At last item, set last pointer here */
135  if (item == suggestions.last) {
136  suggestions.last = newitem;
137  }
138  break;
139  }
140  }
141  /* Reached beginning of list, insert before first */
142  if (cmp < 0) {
143  newitem->next = suggestions.first;
144  suggestions.first->prev = newitem;
145  suggestions.first = newitem;
146  }
147  }
149  suggestions.top = 0;
150 }
151 
152 void texttool_suggest_prefix(const char *prefix, const int prefix_len)
153 {
154  SuggItem *match, *first, *last;
155  int cmp, top = 0;
156 
157  if (!suggestions.first) {
158  return;
159  }
160  if (prefix_len == 0) {
163  return;
164  }
165 
166  first = last = NULL;
167  for (match = suggestions.first; match; match = match->next) {
168  cmp = BLI_strncasecmp(prefix, match->name, prefix_len);
169  if (cmp == 0) {
170  if (!first) {
171  first = match;
172  suggestions.top = top;
173  }
174  }
175  else if (cmp < 0) {
176  if (!last) {
177  last = match->prev;
178  break;
179  }
180  }
181  top++;
182  }
183  if (first) {
184  if (!last) {
185  last = suggestions.last;
186  }
187  suggestions.firstmatch = first;
188  suggestions.lastmatch = last;
189  suggestions.selected = first;
190  }
191  else {
195  suggestions.top = 0;
196  }
197 }
198 
200 {
202 }
203 
205 {
206  return suggestions.firstmatch;
207 }
208 
210 {
211  return suggestions.lastmatch;
212 }
213 
215 {
216  suggestions.selected = sel;
217 }
218 
220 {
221  return suggestions.selected;
222 }
223 
225 {
226  return &suggestions.top;
227 }
228 
229 /*************************/
230 /* Documentation methods */
231 /*************************/
232 
233 void texttool_docs_show(const char *docs)
234 {
235  int len;
236 
237  if (!docs) {
238  return;
239  }
240 
241  len = strlen(docs);
242 
243  if (documentation) {
246  }
247 
248  /* Ensure documentation ends with a '\n' */
249  if (docs[len - 1] != '\n') {
250  documentation = MEM_mallocN(len + 2, "Documentation");
251  memcpy(documentation, docs, len);
252  documentation[len++] = '\n';
253  }
254  else {
255  documentation = MEM_mallocN(len + 1, "Documentation");
256  memcpy(documentation, docs, len);
257  }
258  documentation[len] = '\0';
259 }
260 
261 char *texttool_docs_get(void)
262 {
263  return documentation;
264 }
265 
267 {
268  txttl_free_docs();
269 }
int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:689
_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.
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
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)
uint len