svn merge -r 16369:16454 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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         suggestions.top = 0;
69 }
70
71 static void txttl_free_docs() {
72         if (documentation) {
73                 MEM_freeN(documentation);
74                 documentation = NULL;
75         }
76 }
77
78 /**************************/
79 /* General tool functions */
80 /**************************/
81
82 void free_texttools() {
83         txttl_free_suggest();
84         txttl_free_docs();
85 }
86
87 void texttool_text_set_active(Text *text) {
88         if (activeToolText == text) return;
89         texttool_text_clear();
90         activeToolText = text;
91 }
92
93 void texttool_text_clear() {
94         free_texttools();
95         activeToolText = NULL;
96 }
97
98 short texttool_text_is_active(Text *text) {
99         return activeToolText==text ? 1 : 0;
100 }
101
102 /***************************/
103 /* Suggestion list methods */
104 /***************************/
105
106 void texttool_suggest_add(const char *name, char type) {
107         SuggItem *newitem, *item;
108         int len, cmp;
109
110         newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem");
111         if (!newitem) {
112                 printf("Failed to allocate memory for suggestion.\n");
113                 return;
114         }
115
116         newitem->name = (char *) (newitem + 1);
117         len = strlen(name);
118         strncpy(newitem->name, name, len);
119         newitem->name[len] = '\0';
120         newitem->type = type;
121         newitem->prev = newitem->next = NULL;
122
123         /* Perform simple linear search for ordered storage */
124         if (!suggestions.first || !suggestions.last) {
125                 suggestions.first = suggestions.last = newitem;
126         } else {
127                 cmp = -1;
128                 for (item=suggestions.last; item; item=item->prev) {
129                         cmp = txttl_cmp(name, item->name, len);
130
131                         /* Newitem comes after this item, insert here */
132                         if (cmp >= 0) {
133                                 newitem->prev = item;
134                                 if (item->next)
135                                         item->next->prev = newitem;
136                                 newitem->next = item->next;
137                                 item->next = newitem;
138
139                                 /* At last item, set last pointer here */
140                                 if (item == suggestions.last)
141                                         suggestions.last = newitem;
142                                 break;
143                         }
144                 }
145                 /* Reached beginning of list, insert before first */
146                 if (cmp < 0) {
147                         newitem->next = suggestions.first;
148                         suggestions.first->prev = newitem;
149                         suggestions.first = newitem;
150                 }
151         }
152         suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = NULL;
153         suggestions.top= 0;
154 }
155
156 void texttool_suggest_prefix(const char *prefix) {
157         SuggItem *match, *first, *last;
158         int cmp, len = strlen(prefix), top = 0;
159
160         if (!suggestions.first) return;
161         if (len==0) {
162                 suggestions.selected = suggestions.firstmatch = suggestions.first;
163                 suggestions.lastmatch = suggestions.last;
164                 return;
165         }
166         
167         first = last = NULL;
168         for (match=suggestions.first; match; match=match->next) {
169                 cmp = txttl_cmp(prefix, match->name, len);
170                 if (cmp==0) {
171                         if (!first) {
172                                 first = match;
173                                 suggestions.top = top;
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) last = suggestions.last;
185                 suggestions.firstmatch = first;
186                 suggestions.lastmatch = last;
187                 suggestions.selected = first;
188         } else {
189                 suggestions.firstmatch = NULL;
190                 suggestions.lastmatch = NULL;
191                 suggestions.selected = NULL;
192                 suggestions.top = 0;
193         }
194 }
195
196 void texttool_suggest_clear() {
197         txttl_free_suggest();
198 }
199
200 SuggItem *texttool_suggest_first() {
201         return suggestions.firstmatch;
202 }
203
204 SuggItem *texttool_suggest_last() {
205         return suggestions.lastmatch;
206 }
207
208 void texttool_suggest_select(SuggItem *sel) {
209         suggestions.selected = sel;
210 }
211
212 SuggItem *texttool_suggest_selected() {
213         return suggestions.selected;
214 }
215
216 int *texttool_suggest_top() {
217         return &suggestions.top;
218 }
219
220 /*************************/
221 /* Documentation methods */
222 /*************************/
223
224 void texttool_docs_show(const char *docs) {
225         int len;
226
227         if (!docs) return;
228
229         len = strlen(docs);
230
231         if (documentation) {
232                 MEM_freeN(documentation);
233                 documentation = NULL;
234         }
235
236         /* Ensure documentation ends with a '\n' */
237         if (docs[len-1] != '\n') {
238                 documentation = MEM_mallocN(len+2, "Documentation");
239                 strncpy(documentation, docs, len);
240                 documentation[len++] = '\n';
241         } else {
242                 documentation = MEM_mallocN(len+1, "Documentation");
243                 strncpy(documentation, docs, len);
244         }
245         documentation[len] = '\0';
246 }
247
248 char *texttool_docs_get() {
249         return documentation;
250 }
251
252 void texttool_docs_clear() {
253         txttl_free_docs();
254 }