Updated the textplugin_outliner.py plug-in to use the new Draw.PupTreeMenu and updated this menu to support titles.

This commit is contained in:
2008-07-21 19:11:38 +00:00
parent 434f2172f6
commit 07b8c7e887
3 changed files with 104 additions and 39 deletions

View 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))
return Draw.PupMenu(title+'%t|'+'|'.join(newitems))
subs = []
for c in letters:
subs.append((c, dict[c]))
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)

View 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

View 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,7 +2298,13 @@ 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 {
@@ -2313,6 +2319,7 @@ void toolbox_generic( TBitem *generic_menu )
}
ypos-=20;
}
first= 0;
}
uiBlockSetButmFunc(block, menu->poin, NULL);