2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2008 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2008-06-24 15:25:25 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2011-02-27 20:40:57 +00:00
|
|
|
*/
|
|
|
|
|
|
2023-07-22 11:27:25 +10:00
|
|
|
#include <cctype>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
2008-06-24 15:25:25 +00:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
2015-10-25 17:44:32 +11:00
|
|
|
|
|
|
|
|
#include "BLI_string.h"
|
|
|
|
|
|
2019-11-22 13:29:25 +11:00
|
|
|
#include "BKE_text_suggestions.h" /* Own include. */
|
2008-06-24 15:25:25 +00:00
|
|
|
|
2008-07-21 16:40:32 +00:00
|
|
|
/**********************/
|
|
|
|
|
/* Static definitions */
|
|
|
|
|
/**********************/
|
|
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
static Text *activeToolText = nullptr;
|
|
|
|
|
static SuggList suggestions = {nullptr, nullptr, nullptr, nullptr, nullptr};
|
|
|
|
|
static char *documentation = nullptr;
|
2019-05-01 11:09:22 +10:00
|
|
|
// static int doc_lines = 0;
|
2008-06-24 15:25:25 +00:00
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
static void txttl_free_suggest()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-06-25 13:51:54 +00:00
|
|
|
SuggItem *item, *prev;
|
2012-05-12 16:11:34 +00:00
|
|
|
for (item = suggestions.last; item; item = prev) {
|
2008-06-25 13:51:54 +00:00
|
|
|
prev = item->prev;
|
2008-06-24 15:25:25 +00:00
|
|
|
MEM_freeN(item);
|
2008-06-25 13:51:54 +00:00
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
suggestions.first = suggestions.last = nullptr;
|
|
|
|
|
suggestions.firstmatch = suggestions.lastmatch = nullptr;
|
|
|
|
|
suggestions.selected = nullptr;
|
2008-08-11 11:10:16 +00:00
|
|
|
suggestions.top = 0;
|
2008-06-24 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
static void txttl_free_docs()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2021-08-06 13:59:38 +10:00
|
|
|
MEM_SAFE_FREE(documentation);
|
2008-07-18 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-21 16:40:32 +00:00
|
|
|
/**************************/
|
|
|
|
|
/* General tool functions */
|
|
|
|
|
/**************************/
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
void free_texttools()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-07-23 21:28:48 +00:00
|
|
|
txttl_free_suggest();
|
|
|
|
|
txttl_free_docs();
|
2008-07-18 23:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void texttool_text_set_active(Text *text)
|
|
|
|
|
{
|
2019-04-22 09:39:35 +10:00
|
|
|
if (activeToolText == text) {
|
2008-07-23 21:28:48 +00:00
|
|
|
return;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2008-07-23 21:28:48 +00:00
|
|
|
texttool_text_clear();
|
|
|
|
|
activeToolText = text;
|
2008-07-21 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
void texttool_text_clear()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-07-23 21:28:48 +00:00
|
|
|
free_texttools();
|
2023-07-17 10:46:26 +02:00
|
|
|
activeToolText = nullptr;
|
2008-07-21 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
short texttool_text_is_active(Text *text)
|
|
|
|
|
{
|
2012-05-12 16:11:34 +00:00
|
|
|
return activeToolText == text ? 1 : 0;
|
2008-07-21 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***************************/
|
|
|
|
|
/* Suggestion list methods */
|
|
|
|
|
/***************************/
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void texttool_suggest_add(const char *name, char type)
|
|
|
|
|
{
|
2012-11-14 09:45:15 +00:00
|
|
|
const int len = strlen(name);
|
|
|
|
|
int cmp;
|
2008-07-21 16:40:32 +00:00
|
|
|
SuggItem *newitem, *item;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
newitem = static_cast<SuggItem *>(MEM_mallocN(sizeof(SuggItem) + len + 1, "SuggItem"));
|
2008-06-24 15:25:25 +00:00
|
|
|
if (!newitem) {
|
|
|
|
|
printf("Failed to allocate memory for suggestion.\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-11-14 09:45:15 +00:00
|
|
|
memcpy(newitem->name, name, len + 1);
|
2008-06-24 15:25:25 +00:00
|
|
|
newitem->type = type;
|
2023-07-17 10:46:26 +02:00
|
|
|
newitem->prev = newitem->next = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-07-21 16:40:32 +00:00
|
|
|
/* Perform simple linear search for ordered storage */
|
|
|
|
|
if (!suggestions.first || !suggestions.last) {
|
2008-06-24 15:25:25 +00:00
|
|
|
suggestions.first = suggestions.last = newitem;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2008-07-21 16:40:32 +00:00
|
|
|
cmp = -1;
|
2012-05-12 16:11:34 +00:00
|
|
|
for (item = suggestions.last; item; item = item->prev) {
|
2015-10-25 17:44:32 +11:00
|
|
|
cmp = BLI_strncasecmp(name, item->name, len);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-07-21 16:40:32 +00:00
|
|
|
/* Newitem comes after this item, insert here */
|
|
|
|
|
if (cmp >= 0) {
|
|
|
|
|
newitem->prev = item;
|
2019-04-22 09:39:35 +10:00
|
|
|
if (item->next) {
|
2008-07-21 16:40:32 +00:00
|
|
|
item->next->prev = newitem;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2008-07-21 16:40:32 +00:00
|
|
|
newitem->next = item->next;
|
|
|
|
|
item->next = newitem;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-07-21 16:40:32 +00:00
|
|
|
/* At last item, set last pointer here */
|
2019-04-22 09:39:35 +10:00
|
|
|
if (item == suggestions.last) {
|
2008-07-21 16:40:32 +00:00
|
|
|
suggestions.last = newitem;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2008-07-21 16:40:32 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Reached beginning of list, insert before first */
|
|
|
|
|
if (cmp < 0) {
|
|
|
|
|
newitem->next = suggestions.first;
|
|
|
|
|
suggestions.first->prev = newitem;
|
|
|
|
|
suggestions.first = newitem;
|
|
|
|
|
}
|
2008-06-24 15:25:25 +00:00
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = nullptr;
|
2012-05-12 16:11:34 +00:00
|
|
|
suggestions.top = 0;
|
2008-06-24 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-31 14:49:27 +00:00
|
|
|
void texttool_suggest_prefix(const char *prefix, const int prefix_len)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-06-24 15:25:25 +00:00
|
|
|
SuggItem *match, *first, *last;
|
2012-12-31 14:49:27 +00:00
|
|
|
int cmp, top = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (!suggestions.first) {
|
2008-06-24 15:25:25 +00:00
|
|
|
return;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2012-12-31 14:49:27 +00:00
|
|
|
if (prefix_len == 0) {
|
2008-06-25 13:51:54 +00:00
|
|
|
suggestions.selected = suggestions.firstmatch = suggestions.first;
|
2008-06-24 15:25:25 +00:00
|
|
|
suggestions.lastmatch = suggestions.last;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
first = last = nullptr;
|
2012-05-12 16:11:34 +00:00
|
|
|
for (match = suggestions.first; match; match = match->next) {
|
2015-10-25 17:44:32 +11:00
|
|
|
cmp = BLI_strncasecmp(prefix, match->name, prefix_len);
|
2012-05-12 16:11:34 +00:00
|
|
|
if (cmp == 0) {
|
2008-08-11 11:10:16 +00:00
|
|
|
if (!first) {
|
2008-06-24 15:25:25 +00:00
|
|
|
first = match;
|
2008-08-11 11:10:16 +00:00
|
|
|
suggestions.top = top;
|
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 16:11:34 +00:00
|
|
|
else if (cmp < 0) {
|
2008-06-24 15:25:25 +00:00
|
|
|
if (!last) {
|
|
|
|
|
last = match->prev;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-11 11:10:16 +00:00
|
|
|
top++;
|
2008-06-24 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
if (first) {
|
2019-04-22 09:39:35 +10:00
|
|
|
if (!last) {
|
2008-06-24 15:25:25 +00:00
|
|
|
last = suggestions.last;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2008-07-18 23:12:19 +00:00
|
|
|
suggestions.firstmatch = first;
|
2008-06-24 15:25:25 +00:00
|
|
|
suggestions.lastmatch = last;
|
2008-07-18 23:12:19 +00:00
|
|
|
suggestions.selected = first;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-07-17 10:46:26 +02:00
|
|
|
suggestions.firstmatch = nullptr;
|
|
|
|
|
suggestions.lastmatch = nullptr;
|
|
|
|
|
suggestions.selected = nullptr;
|
2008-08-11 11:10:16 +00:00
|
|
|
suggestions.top = 0;
|
2008-06-24 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
void texttool_suggest_clear()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-07-23 21:28:48 +00:00
|
|
|
txttl_free_suggest();
|
2008-07-21 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
SuggItem *texttool_suggest_first()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-06-24 15:25:25 +00:00
|
|
|
return suggestions.firstmatch;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
SuggItem *texttool_suggest_last()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-06-24 15:25:25 +00:00
|
|
|
return suggestions.lastmatch;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void texttool_suggest_select(SuggItem *sel)
|
|
|
|
|
{
|
2008-07-23 21:28:48 +00:00
|
|
|
suggestions.selected = sel;
|
2008-06-24 15:25:25 +00:00
|
|
|
}
|
2008-06-25 13:51:54 +00:00
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
SuggItem *texttool_suggest_selected()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-07-23 21:28:48 +00:00
|
|
|
return suggestions.selected;
|
2008-06-25 13:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:18:07 +10:00
|
|
|
int *texttool_suggest_top()
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2008-08-11 11:10:16 +00:00
|
|
|
return &suggestions.top;
|
|
|
|
|
}
|