Added UI for suggestions list. Works with arrow-keys and mouse wheel, accept with...
[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): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34 #include "BLI_blenlib.h"
35 #include "DNA_text_types.h"
36 #include "BKE_text.h"
37 #include "BKE_suggestions.h"
38
39 static SuggList suggestions= {NULL, NULL, NULL, NULL};
40 static Text *suggText = NULL;
41
42 void free_suggestions() {
43         SuggItem *item, *prev;
44         for (item = suggestions.last; item; item=prev) {
45                 prev = item->prev;
46                 MEM_freeN(item);
47         }
48         suggestions.first = suggestions.last = NULL;
49         suggestions.firstmatch = suggestions.lastmatch = NULL;
50         suggestions.selected = NULL;
51 }
52
53 void suggest_add(const char *name, char type) {
54         SuggItem *newitem;
55
56         newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem");
57         if (!newitem) {
58                 printf("Failed to allocate memory for suggestion.\n");
59                 return;
60         }
61
62         newitem->name = (char *) (newitem + 1);
63         strcpy(newitem->name, name);
64         newitem->type = type;
65         newitem->prev = newitem->next = NULL;
66
67         if (!suggestions.first) {
68                 suggestions.first = suggestions.last = newitem;
69                 suggestions.selected = newitem;
70         } else {
71                 newitem->prev = suggestions.last;
72                 suggestions.last->next = newitem;
73                 suggestions.last = newitem;
74         }
75 }
76
77 void suggest_prefix(const char *prefix) {
78         SuggItem *match, *first, *last;
79         int cmp, len = strlen(prefix);
80
81         if (!suggestions.first) return;
82         if (len==0) {
83                 suggestions.selected = suggestions.firstmatch = suggestions.first;
84                 suggestions.lastmatch = suggestions.last;
85                 return;
86         }
87         
88         first = last = NULL;
89         for (match=suggestions.first; match; match=match->next) {
90                 cmp = strncmp(prefix, match->name, len);
91                 if (cmp==0) {
92                         if (!first)
93                                 first = match;
94                 } else if (cmp<0) {
95                         if (!last) {
96                                 last = match->prev;
97                                 break;
98                         }
99                 }
100         }
101         if (first) {
102                 if (!last) last = suggestions.last;
103                 suggestions.selected = suggestions.firstmatch = first;
104                 suggestions.lastmatch = last;
105         } else {
106                 suggestions.selected = suggestions.firstmatch = suggestions.lastmatch = NULL;
107         }
108 }
109
110 SuggItem *suggest_first() {
111         return suggestions.firstmatch;
112 }
113
114 SuggItem *suggest_last() {
115         return suggestions.lastmatch;
116 }
117
118 void suggest_set_text(Text *text) {
119         suggText = text;
120 }
121
122 void suggest_clear_text() {
123         free_suggestions();
124         suggText = NULL;
125 }
126
127 short suggest_is_active(Text *text) {
128         return suggText==text ? 1 : 0;
129 }
130
131 void suggest_set_selected(SuggItem *sel) {
132         suggestions.selected = sel;
133 }
134
135 SuggItem *suggest_get_selected() {
136         return suggestions.selected;
137 }