Blender V5.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
8
9#include <cctype>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_string.hh"
17
18#include "BKE_text_suggestions.h" /* Own include. */
19
20namespace blender {
21
22/**********************/
23/* Static definitions */
24/**********************/
25
26static Text *activeToolText = nullptr;
27static SuggList suggestions = {nullptr, nullptr, nullptr, nullptr, nullptr};
28static char *documentation = nullptr;
29// static int doc_lines = 0;
30
31static void txttl_free_suggest()
32{
33 SuggItem *item, *prev;
34 for (item = suggestions.last; item; item = prev) {
35 prev = item->prev;
36 MEM_delete(item);
37 }
40 suggestions.selected = nullptr;
41 suggestions.top = 0;
42}
43
44static void txttl_free_docs()
45{
46 MEM_SAFE_DELETE(documentation);
47}
48
49/**************************/
50/* General tool functions */
51/**************************/
52
58
60{
61 if (activeToolText == text) {
62 return;
63 }
65 activeToolText = text;
66}
67
69{
71 activeToolText = nullptr;
72}
73
75{
76 return activeToolText == text ? 1 : 0;
77}
78
79/***************************/
80/* Suggestion list methods */
81/***************************/
82
83void texttool_suggest_add(const char *name, char type)
84{
85 const int len = strlen(name);
86 int cmp;
87 SuggItem *newitem, *item;
88
89 newitem = static_cast<SuggItem *>(MEM_new_uninitialized(sizeof(SuggItem) + len + 1, "SuggItem"));
90 if (!newitem) {
91 printf("Failed to allocate memory for suggestion.\n");
92 return;
93 }
94
95 memcpy(newitem->name, name, len + 1);
96 newitem->type = type;
97 newitem->prev = newitem->next = nullptr;
98
99 /* Perform simple linear search for ordered storage */
101 suggestions.first = suggestions.last = newitem;
102 }
103 else {
104 cmp = -1;
105 for (item = suggestions.last; item; item = item->prev) {
106 cmp = BLI_strncasecmp(name, item->name, len);
107
108 /* Newitem comes after this item, insert here */
109 if (cmp >= 0) {
110 newitem->prev = item;
111 if (item->next) {
112 item->next->prev = newitem;
113 }
114 newitem->next = item->next;
115 item->next = newitem;
116
117 /* At last item, set last pointer here */
118 if (item == suggestions.last) {
119 suggestions.last = newitem;
120 }
121 break;
122 }
123 }
124 /* Reached beginning of list, insert before first */
125 if (cmp < 0) {
126 newitem->next = suggestions.first;
127 suggestions.first->prev = newitem;
128 suggestions.first = newitem;
129 }
130 }
132 suggestions.top = 0;
133}
134
135void texttool_suggest_prefix(const char *prefix, const int prefix_len)
136{
137 SuggItem *match, *first, *last;
138 int cmp, top = 0;
139
140 if (!suggestions.first) {
141 return;
142 }
143 if (prefix_len == 0) {
146 return;
147 }
148
149 first = last = nullptr;
150 for (match = suggestions.first; match; match = match->next) {
151 cmp = BLI_strncasecmp(prefix, match->name, prefix_len);
152 if (cmp == 0) {
153 if (!first) {
154 first = match;
155 suggestions.top = top;
156 }
157 }
158 else if (cmp < 0) {
159 if (!last) {
160 last = match->prev;
161 break;
162 }
163 }
164 top++;
165 }
166 if (first) {
167 if (!last) {
168 last = suggestions.last;
169 }
170 suggestions.firstmatch = first;
171 suggestions.lastmatch = last;
172 suggestions.selected = first;
173 }
174 else {
175 suggestions.firstmatch = nullptr;
176 suggestions.lastmatch = nullptr;
177 suggestions.selected = nullptr;
178 suggestions.top = 0;
179 }
180}
181
186
191
196
198{
199 suggestions.selected = sel;
200}
201
206
208{
209 return &suggestions.top;
210}
211
212} // namespace blender
Read Blender memory allocation and freeing.
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void texttool_suggest_prefix(const char *prefix, int prefix_len)
static Text * activeToolText
SuggItem * texttool_suggest_first()
void texttool_suggest_clear()
SuggItem * texttool_suggest_last()
int char char int int BLI_strncasecmp(const char *s1, const char *s2, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
short texttool_text_is_active(struct Text *text)
static void txttl_free_docs()
void texttool_suggest_add(const char *name, char type)
static void txttl_free_suggest()
int * texttool_suggest_top()
SuggItem * texttool_suggest_selected()
void free_texttools()
static char * documentation
static SuggList suggestions
void texttool_suggest_select(SuggItem *sel)
void texttool_text_set_active(struct Text *text)
void texttool_text_clear()
struct SuggItem * prev
struct SuggItem * next