Blender V4.3
text_suggestions.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cctype>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_string.h"
17
18#include "BKE_text_suggestions.h" /* Own include. */
19
20/**********************/
21/* Static definitions */
22/**********************/
23
24static Text *activeToolText = nullptr;
25static SuggList suggestions = {nullptr, nullptr, nullptr, nullptr, nullptr};
26static char *documentation = nullptr;
27// static int doc_lines = 0;
28
29static void txttl_free_suggest()
30{
31 SuggItem *item, *prev;
32 for (item = suggestions.last; item; item = prev) {
33 prev = item->prev;
34 MEM_freeN(item);
35 }
38 suggestions.selected = nullptr;
39 suggestions.top = 0;
40}
41
42static void txttl_free_docs()
43{
45}
46
47/**************************/
48/* General tool functions */
49/**************************/
50
56
58{
59 if (activeToolText == text) {
60 return;
61 }
63 activeToolText = text;
64}
65
67{
69 activeToolText = nullptr;
70}
71
73{
74 return activeToolText == text ? 1 : 0;
75}
76
77/***************************/
78/* Suggestion list methods */
79/***************************/
80
81void 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 = static_cast<SuggItem *>(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 = nullptr;
96
97 /* Perform simple linear search for ordered storage */
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
133void 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 = nullptr;
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;
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 {
173 suggestions.firstmatch = nullptr;
174 suggestions.lastmatch = nullptr;
175 suggestions.selected = nullptr;
176 suggestions.top = 0;
177 }
178}
179
184
189
194
196{
197 suggestions.selected = sel;
198}
199
204
206{
207 return &suggestions.top;
208}
int char char int int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define printf
int len
uint top
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
struct SuggItem * prev
struct SuggItem * next
SuggItem * first
SuggItem * firstmatch
SuggItem * selected
SuggItem * lastmatch
SuggItem * last
static void txttl_free_docs()
void texttool_text_set_active(Text *text)
static void txttl_free_suggest()
SuggItem * texttool_suggest_first()
int * texttool_suggest_top()
void texttool_suggest_select(SuggItem *sel)
void texttool_suggest_prefix(const char *prefix, const int prefix_len)
void texttool_suggest_clear()
static Text * activeToolText
void texttool_suggest_add(const char *name, char type)
void texttool_text_clear()
SuggItem * texttool_suggest_last()
static SuggList suggestions
short texttool_text_is_active(Text *text)
SuggItem * texttool_suggest_selected()
void free_texttools()
static char * documentation