resolve some compiler warnings with intel c/c++ compiler
[blender.git] / source / blender / blenkernel / intern / suggestions.c
index e2c951f228490d982dc1ba4f9e8b33ccc34028ff..6530909336ccb50042f01b775e46fb515cc57a80 100644 (file)
 #include "BKE_text.h"
 #include "BKE_suggestions.h"
 
+/**********************/
+/* Static definitions */
+/**********************/
+
+static Text *activeToolText = NULL;
 static SuggList suggestions = {NULL, NULL, NULL, NULL, NULL};
-static Text *suggText = NULL;
-static SuggItem *lastInsert = NULL;
 static char *documentation = NULL;
-static int doc_lines = 0;
+//static int doc_lines = 0;
 
-static int suggest_cmp(const char *first, const char *second, int len) {       
+static int txttl_cmp(const char *first, const char *second, int len) { 
        int cmp, i;
        for (cmp=0, i=0; i<len; i++) {
-               if (cmp= toupper(first[i])-toupper(second[i])) {
+               if ( (cmp= toupper(first[i])-toupper(second[i])) ) {
                        break;
                }
        }
        return cmp;
 }
 
-static void sugg_free() {
+static void txttl_free_suggest() {
        SuggItem *item, *prev;
        for (item = suggestions.last; item; item=prev) {
                prev = item->prev;
@@ -62,22 +65,47 @@ static void sugg_free() {
        suggestions.first = suggestions.last = NULL;
        suggestions.firstmatch = suggestions.lastmatch = NULL;
        suggestions.selected = NULL;
+       suggestions.top = 0;
 }
 
-static void docs_free() {
+static void txttl_free_docs() {
        if (documentation) {
                MEM_freeN(documentation);
                documentation = NULL;
        }
 }
 
-void free_suggestions() {
-       sugg_free();
-       docs_free();
+/**************************/
+/* General tool functions */
+/**************************/
+
+void free_texttools() {
+       txttl_free_suggest();
+       txttl_free_docs();
+}
+
+void texttool_text_set_active(Text *text) {
+       if (activeToolText == text) return;
+       texttool_text_clear();
+       activeToolText = text;
 }
 
-void suggest_add(const char *name, char type) {
-       SuggItem *newitem;
+void texttool_text_clear() {
+       free_texttools();
+       activeToolText = NULL;
+}
+
+short texttool_text_is_active(Text *text) {
+       return activeToolText==text ? 1 : 0;
+}
+
+/***************************/
+/* Suggestion list methods */
+/***************************/
+
+void texttool_suggest_add(const char *name, char type) {
+       SuggItem *newitem, *item;
+       int len, cmp;
 
        newitem = MEM_mallocN(sizeof(SuggItem) + strlen(name) + 1, "SuggestionItem");
        if (!newitem) {
@@ -86,23 +114,48 @@ void suggest_add(const char *name, char type) {
        }
 
        newitem->name = (char *) (newitem + 1);
-       strcpy(newitem->name, name);
+       len = strlen(name);
+       strncpy(newitem->name, name, len);
+       newitem->name[len] = '\0';
        newitem->type = type;
        newitem->prev = newitem->next = NULL;
 
-       if (!suggestions.first) {
+       /* Perform simple linear search for ordered storage */
+       if (!suggestions.first || !suggestions.last) {
                suggestions.first = suggestions.last = newitem;
        } else {
-               newitem->prev = suggestions.last;
-               suggestions.last->next = newitem;
-               suggestions.last = newitem;
+               cmp = -1;
+               for (item=suggestions.last; item; item=item->prev) {
+                       cmp = txttl_cmp(name, item->name, len);
+
+                       /* Newitem comes after this item, insert here */
+                       if (cmp >= 0) {
+                               newitem->prev = item;
+                               if (item->next)
+                                       item->next->prev = newitem;
+                               newitem->next = item->next;
+                               item->next = newitem;
+
+                               /* At last item, set last pointer here */
+                               if (item == suggestions.last)
+                                       suggestions.last = newitem;
+                               break;
+                       }
+               }
+               /* Reached beginning of list, insert before first */
+               if (cmp < 0) {
+                       newitem->next = suggestions.first;
+                       suggestions.first->prev = newitem;
+                       suggestions.first = newitem;
+               }
        }
-       suggestions.selected = NULL;
+       suggestions.firstmatch = suggestions.lastmatch = suggestions.selected = NULL;
+       suggestions.top= 0;
 }
 
-void suggest_prefix(const char *prefix) {
+void texttool_suggest_prefix(const char *prefix) {
        SuggItem *match, *first, *last;
-       int cmp, len = strlen(prefix);
+       int cmp, len = strlen(prefix), top = 0;
 
        if (!suggestions.first) return;
        if (len==0) {
@@ -113,16 +166,19 @@ void suggest_prefix(const char *prefix) {
        
        first = last = NULL;
        for (match=suggestions.first; match; match=match->next) {
-               cmp = suggest_cmp(prefix, match->name, len);
+               cmp = txttl_cmp(prefix, match->name, len);
                if (cmp==0) {
-                       if (!first)
+                       if (!first) {
                                first = match;
+                               suggestions.top = top;
+                       }
                } else if (cmp<0) {
                        if (!last) {
                                last = match->prev;
                                break;
                        }
                }
+               top++;
        }
        if (first) {
                if (!last) last = suggestions.last;
@@ -133,43 +189,39 @@ void suggest_prefix(const char *prefix) {
                suggestions.firstmatch = NULL;
                suggestions.lastmatch = NULL;
                suggestions.selected = NULL;
+               suggestions.top = 0;
        }
 }
 
-SuggItem *suggest_first() {
-       return suggestions.firstmatch;
-}
-
-SuggItem *suggest_last() {
-       return suggestions.lastmatch;
-}
-
-void suggest_set_active(Text *text) {
-       if (suggText == text) return;
-       suggest_clear_active();
-       suggText = text;
+void texttool_suggest_clear() {
+       txttl_free_suggest();
 }
 
-void suggest_clear_active() {
-       free_suggestions();
-       suggText = NULL;
+SuggItem *texttool_suggest_first() {
+       return suggestions.firstmatch;
 }
 
-short suggest_is_active(Text *text) {
-       return suggText==text ? 1 : 0;
+SuggItem *texttool_suggest_last() {
+       return suggestions.lastmatch;
 }
 
-void suggest_set_selected(SuggItem *sel) {
+void texttool_suggest_select(SuggItem *sel) {
        suggestions.selected = sel;
 }
 
-SuggItem *suggest_get_selected() {
+SuggItem *texttool_suggest_selected() {
        return suggestions.selected;
 }
 
+int *texttool_suggest_top() {
+       return &suggestions.top;
+}
+
+/*************************/
 /* Documentation methods */
+/*************************/
 
-void suggest_documentation(const char *docs) {
+void texttool_docs_show(const char *docs) {
        int len;
 
        if (!docs) return;
@@ -193,10 +245,10 @@ void suggest_documentation(const char *docs) {
        documentation[len] = '\0';
 }
 
-char *suggest_get_docs() {
+char *texttool_docs_get() {
        return documentation;
 }
 
-void suggest_clear_docs() {
-       docs_free();
+void texttool_docs_clear() {
+       txttl_free_docs();
 }