Refactor: Renamed text tool methods (suggestions and docs) for clarity and consistency.
[blender.git] / source / blender / blenkernel / intern / suggestions.c
1 /**
2  * $Id: $
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008, Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Ian Thompson.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "MEM_guardedalloc.h"
35 #include "BLI_blenlib.h"
36 #include "DNA_text_types.h"
37 #include "BKE_text.h"
38 #include "BKE_suggestions.h"
39
40 /**********************/
41 /* Static definitions */
42 /**********************/
43
44 static Text *activeToolText = NULL;
45 static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL};
46 static char *documentation = NULL;
47 static int doc_lines = 0;
48
49 static int txttl_cmp(const char *first, const char *second, int len) {  
50         int cmp, i;
51         for (cmp=0, i=0; i<len; i++) {
52                 if (cmp= toupper(first[i])-toupper(second[i])) {
53                         break;
54                 }
55         }
56         return cmp;
57 }
58
59 static void txttl_free_suggest() {
60         SuggItem *item, *prev;
61         for (item = suggestions.last; item; item=prev) {
62                 prev = item->prev;
63                 MEM_freeN(item);
64         }
65         suggestions.first = suggestions.last = NULL;
66         suggestions.firstmatch = suggestions.lastmatch = NULL;
67         suggestions.selected = NULL;
68 }
69
70 static void txttl_free_docs() {
71         if (documentation) {
72                 MEM_freeN(documentation);
73                 documentation = NULL;
74         }
75 }
76
77 /**************************/
78 /* General tool functions */
79 /**************************/
80
81 void free_texttools() {
82         txttl_free_suggest();
83         txttl_free_docs();
84 }
85
86 void texttool_text_set_active(Text *text) {
87         if (activeToolText == text) return;
88         texttool_text_clear();
89         activeToolText = text;
90 }
91
92 void texttool_text_clear() {
93         free_texttools();
94         activeToolText = NULL;
95 }
96
97 short texttool_text_is_active(Text *text) {
98         return activeToolText==text ? 1 : 0;
99 }
100
101 /***************************/
102 /* Suggestion list methods */
103 /***************************/
104
105 void texttool_suggest_add(const char *name, char type) {
106         SuggItem *newitem, *item;
107         int len, cmp;
108
109         newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem");
110         if (!newitem) {
111                 printf("Failed to allocate memory for suggestion.\n");
112                 return;
113         }
114
115         newitem->name = (char *) (newitem + 1);
116         len = strlen(name);
117         strncpy(newitem->name, name, len);
118         newitem->name[len] = '\0';
119         newitem->type = type;
120         newitem->prev = newitem->next = NULL;
121
122         /* Perform simple linear search for ordered storage */
123         if (!suggestions.first || !suggestions.last) {
124                 suggestions.first = suggestions.last = newitem;
125         } else {
126                 cmp = -1;
127                 for (item=suggestions.last; item; item=item->prev) {
128                         cmp = txttl_cmp(name, item->name, len);
129
130                         /* Newitem comes after this item, insert here */
131                         if (cmp >= 0) {
132                                 newitem->prev = item;
133                                 if (item->next)
134                                         item->next->prev = newitem;
135                                 newitem->next = item->next;
136                                 item->next = newitem;
137
138                                 /* At last item, set last pointer here */
139                                 if (item == suggestions.last)
140                                         suggestions.last = newitem;
141                                 break;
142                         }
143                 }
144                 /* Reached beginning of list, insert before first */
145                 if (cmp < 0) {
146                         newitem->next = suggestions.first;
147                         suggestions.first->prev = newitem;
148                         suggestions.first = newitem;
149                 }
150         }
151         suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = NULL;
152 }
153
154 void texttool_suggest_prefix(const char *prefix) {
155         SuggItem *match, *first, *last;
156         int cmp, len = strlen(prefix);
157
158         if (!suggestions.first) return;
159         if (len==0) {
160                 suggestions.selected = suggestions.firstmatch = suggestions.first;
161                 suggestions.lastmatch = suggestions.last;
162                 return;
163         }
164         
165         first = last = NULL;
166         for (match=suggestions.first; match; match=match->next) {
167                 cmp = txttl_cmp(prefix, match->name, len);
168                 if (cmp==0) {
169                         if (!first)
170                                 first = match;
171                 } else if (cmp<0) {
172                         if (!last) {
173                                 last = match->prev;
174                                 break;
175                         }
176                 }
177         }
178         if (first) {
179                 if (!last) last = suggestions.last;
180                 suggestions.firstmatch = first;
181                 suggestions.lastmatch = last;
182                 suggestions.selected = first;
183         } else {
184                 suggestions.firstmatch = NULL;
185                 suggestions.lastmatch = NULL;
186                 suggestions.selected = NULL;
187         }
188 }
189
190 void texttool_suggest_clear() {
191         txttl_free_suggest();
192 }
193
194 SuggItem *texttool_suggest_first() {
195         return suggestions.firstmatch;
196 }
197
198 SuggItem *texttool_suggest_last() {
199         return suggestions.lastmatch;
200 }
201
202 void texttool_suggest_select(SuggItem *sel) {
203         suggestions.selected = sel;
204 }
205
206 SuggItem *texttool_suggest_selected() {
207         return suggestions.selected;
208 }
209
210 /*************************/
211 /* Documentation methods */
212 /*************************/
213
214 void texttool_docs_show(const char *docs) {
215         int len;
216
217         if (!docs) return;
218
219         len = strlen(docs);
220
221         if (documentation) {
222                 MEM_freeN(documentation);
223                 documentation = NULL;
224         }
225
226         /* Ensure documentation ends with a '\n' */
227         if (docs[len-1] != '\n') {
228                 documentation = MEM_mallocN(len+2, "Documentation");
229                 strncpy(documentation, docs, len);
230                 documentation[len++] = '\n';
231         } else {
232                 documentation = MEM_mallocN(len+1, "Documentation");
233                 strncpy(documentation, docs, len);
234         }
235         documentation[len] = '\0';
236 }
237
238 char *texttool_docs_get() {
239         return documentation;
240 }
241
242 void texttool_docs_clear() {
243         txttl_free_docs();
244 }