Updated the textplugin_outliner.py plug-in to use the new Draw.PupTreeMenu and update...
authorIan Thompson <quornian@googlemail.com>
Mon, 21 Jul 2008 19:11:38 +0000 (19:11 +0000)
committerIan Thompson <quornian@googlemail.com>
Mon, 21 Jul 2008 19:11:38 +0000 (19:11 +0000)
release/scripts/textplugin_outliner.py
source/blender/python/api2_2x/doc/Draw.py
source/blender/src/toolbox.c

index 64750b6ba1374236af640ea16a59fc66e1143b8b..6dbb86e3b670303543d0784ad7f9a95421cfc12e 100644 (file)
@@ -16,62 +16,118 @@ try:
 except ImportError:
        OK = False
 
-def do_long_menu(title, items):
+def make_menu(items, eventoffs):
        n = len(items)
        if n < 20:
-               return Draw.PupMenu(title+'%t|'+'|'.join(items))
+               return [(items[i], i+1+eventoffs) for i in range(len(items))]
        
        letters = []
-       check = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_' # Cannot start 0-9 so just letters
+       check = 'abcdefghijklmnopqrstuvwxyz_' # Names cannot start 0-9
        for c in check:
                for item in items:
-                       if item[0].upper() == c:
+                       if item[0].lower() == c:
                                letters.append(c)
                                break
        
-       i = Draw.PupMenu(title+'%t|'+'|'.join(letters))
-       if i < 1:
-               return i
-       
-       c = letters[i-1]
-       newitems = []
-       
+       dict = {}
        i = 0
        for item in items:
                i += 1
-               if item[0].upper() == c:
-                       newitems.append(item+'%x'+str(i))
+               c = item[0].lower()
+               if not dict.has_key(c): dict[c] = []
+               dict[c].append((item, i+eventoffs))
+       
+       subs = []
+       for c in letters:
+               subs.append((c, dict[c]))
        
-       return Draw.PupMenu(title+'%t|'+'|'.join(newitems))
+       return subs
+
+def find_word(txt, word):
+       i = 0
+       for line in txt.asLines():
+               c = line.find(word)
+               if c != -1:
+                       txt.setCursorPos(i, c)
+                       break
+               i += 1
 
 def main():
        txt = bpy.data.texts.active
        if not txt:
                return
        
-       items = []
-       i = Draw.PupMenu('Outliner%t|Classes|Defs|Variables')
-       if i < 1: return
+       # Identify word under cursor
+       if get_context(txt) == CTX_NORMAL:
+               line, c = current_line(txt)
+               start = c-1
+               end = c
+               while start >= 0:
+                       if not line[start].lower() in 'abcdefghijklmnopqrstuvwxyz0123456789_':
+                               break
+                       start -= 1
+               while end < len(line):
+                       if not line[end].lower() in 'abcdefghijklmnopqrstuvwxyz0123456789_':
+                               break
+                       end += 1
+               word = line[start+1:end]
+               if word in KEYWORDS:
+                       word = None
+       else:
+               word = None
        
        script = get_cached_descriptor(txt)
-       if i == 1:
-               type = script.classes
-       elif i == 2:
-               type = script.defs
-       elif i == 3:
-               type = script.vars
-       else:
-               return
-       items.extend(type.keys())
-       items.sort(cmp = suggest_cmp)
-       i = do_long_menu('Outliner', items)
-       if i < 1:
+       items = []
+       desc = None
+       
+       tmp = script.classes.keys()
+       tmp.sort(cmp = suggest_cmp)
+       class_menu = make_menu(tmp, len(items))
+       class_menu_length = len(tmp)
+       items.extend(tmp)
+       
+       tmp = script.defs.keys()
+       tmp.sort(cmp = suggest_cmp)
+       defs_menu = make_menu(tmp, len(items))
+       defs_menu_length = len(tmp)
+       items.extend(tmp)
+       
+       tmp = script.vars.keys()
+       tmp.sort(cmp = suggest_cmp)
+       vars_menu = make_menu(tmp, len(items))
+       vars_menu_length = len(tmp)
+       items.extend(tmp)
+       
+       menu = [('Outliner%t', 0),
+                       ('Classes', class_menu),
+                       ('Functions', defs_menu),
+                       ('Variables', vars_menu)]
+       if word:
+               menu.extend([None, ('Locate', [(word, -10)])])
+       
+       i = Draw.PupTreeMenu(menu)
+       if i == -1:
                return
        
-       try:
-               desc = type[items[i-1]]
-       except:
-               desc = None
+       # Chosen to search for word under cursor
+       if i == -10:
+               if script.classes.has_key(word):
+                       desc = script.classes[word]
+               elif script.defs.has_key(word):
+                       desc = script.defs[word]
+               elif script.vars.has_key(word):
+                       desc = script.vars[word]
+               else:
+                       find_word(txt, word)
+                       return
+       else:
+               i -= 1
+               if i < class_menu_length:
+                       desc = script.classes[items[i]]
+               elif i < class_menu_length + defs_menu_length:
+                       desc = script.defs[items[i]]
+               elif i < class_menu_length + defs_menu_length + vars_menu_length:
+                       desc = script.vars[items[i]]
        
        if desc:
                txt.setCursorPos(desc.lineno-1, 0)
index 18234754315057faad02f199517914f0f6aad9f0..97e2279790201cfdb972b36322121fca84081aa6 100644 (file)
@@ -357,12 +357,14 @@ def PupTreeMenu( menu ):
        """
        Create a popup menu tree.
        
-       Each item in the list is a menu item - (str, event), separator - None or submenu - (str, [...]).
+       Each item in the list is: a menu item - (str, event); a separator - None;
+       or submenu - (str, [...]).
        
-       Submenus list uses the same syntax as the menu list.
+       Submenus list uses the same syntax as the menu list. To add a title to the
+       main menu, end the first entry str with '%t' - the event is ignored.
 
        Example::
-               result = Draw.PupTreeMenu( [ ("Menu Item 1", 10), ("Menu Item 2", 12), ("SubMenu", [("Menu Item 3", 100), ("MenuItem4", 101) ]  ) ] )
+               result = Draw.PupTreeMenu( [ ("Title%t", 0), ("Menu Item 1", 10), ("Menu Item 2", 12), ("SubMenu", [("Menu Item 3", 100), ("MenuItem4", 101) ]  ) ] )
        
        @type menu: string
        @param menu: A menu list
index 94d38ee16353e9d24c277b33554d0287b467dfff..94ab289f71661716a8471169d8e55723f18bdaad 100644 (file)
@@ -2277,7 +2277,7 @@ void toolbox_generic( TBitem *generic_menu )
        uiBlock *block;
        uiBut *but;
        TBitem *menu;
-       int dx=96;
+       int dx=96, first=1, len;
        short event, mval[2];
        long ypos = -5;
        
@@ -2298,11 +2298,17 @@ void toolbox_generic( TBitem *generic_menu )
        
        /* Add the menu */
        for (menu = generic_menu; menu->icon != -1; menu++) {
-               if(strcmp(menu->name, "SEPR")==0) {
+               if (first && (len=strlen(menu->name)) > 2 && menu->name[len-2]=='%' && menu->name[len-1]=='t') {
+                       menu->name[len-2] = '\0';
+                       uiSetCurFont(block, UI_HELVB);
+                       uiDefIconTextBut(block, LABEL, 0, ICON_BLANK1, menu->name, mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 19, NULL, 0.0, 0.0, 0, 0, "");
+                       uiSetCurFont(block, UI_HELV);
+                       ypos-=20;
+               } else if(strcmp(menu->name, "SEPR")==0) {
                        uiDefBut(block, SEPR, 0, "", mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 6, NULL, 0.0, 0.0, 0, 0, "");
                        ypos-=6;
                } else {
-                        if (menu->poin) {
+                       if (menu->poin) {
                                but=uiDefIconTextBlockBut(block, tb_makemenu, menu->poin, ICON_RIGHTARROW_THIN, menu->name, mval[0]+tb_mainx,mval[1]+tb_mainy+ypos+5, dx, 19, "");
                                uiButSetFlag(but, UI_MAKE_RIGHT);
                        
@@ -2313,6 +2319,7 @@ void toolbox_generic( TBitem *generic_menu )
                        }
                        ypos-=20;
                }
+               first= 0;
        }
        
        uiBlockSetButmFunc(block, menu->poin, NULL);