#!/usr/bin/env python
#
# $Id: HelpTree.py,v 1.3 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Use the NavigableTree class to build a HelpTree class.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: HelpTree.py,v $',
'rcs_id' : '$Id: HelpTree.py,v 1.3 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sun, 01-Apr-2001 17:49:50 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.3 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
import Tkinter, Pmw
import string
#
# Import Local modules
#
import TreeExplorer
import NavigableTree
#
# Module
#
def move_text_left(input_text):
"""Un-indent help messages.
Since multi-line strings embedded in programs will have unnatural
(and potentially undesired) indention, they need to be adjusted
before they are added to the help system.
To prevent a help string from losing all indention, place a
vertical bar character ('|') at the point on the line where the
left margin should be. The | will be replaced with a space
character.
"""
if not input_text: return input_text
lines = string.split(input_text, '\n')
lines = map(string.strip, lines)
formatted_lines = []
for line in lines:
if not line:
pass
elif line[0] == '|':
line = ' %s' % line[1:]
formatted_lines.append(line)
return string.join(formatted_lines, '\n')
class HelpTree(NavigableTree.NavigableTree):
"""A data structure to hold help text for a GUI application.
"""
nodePathSep=':'
def format_text(self, input_text):
"""Fix up the text so it can be displayed.
Remove the first whitespace character from the input text, since it
probably comes from code that was indented.
"""
return move_text_left(input_text)
def insert_node(self, node_path, help_text=''):
"""Insert a help text node into the tree.
The new node is returned.
Arguments
'node_path' -- A string with node names separated by the
nodePathSep value for the class. The path is assumed to
start at the current node, so the name of the current node
should not be inserted into the beginning of the path.
'help_text' -- A string which should be displayed as the
help string for the node.
"""
#print 'inserting "%s"->%s' % (node_path, help_text)
if not node_path: return
#
# Split up the path
#
node_path_parts = string.split(node_path, self.nodePathSep)
if not node_path_parts: return
remaining_path = string.join(node_path_parts[1:], self.nodePathSep)
#
# Find the insertion point
#
children = self.children()
insertion_point = filter(lambda x, n=node_path_parts[0]:x.name == n, children)
if not insertion_point:
# create a new node
if remaining_path:
newNode = self.__class__(name=node_path_parts[0], parent=self)
else:
formatted_help_text = self.format_text(help_text)
newNode = self.__class__(name=node_path_parts[0],
data=formatted_help_text, parent=self)
self.add_children([ newNode ])
else:
newNode = insertion_point[0]
if not remaining_path:
formatted_help_text = self.format_text(help_text)
if newNode.data:
newNode.data = '%s\n%s' % (newNode.data, formatted_help_text)
else:
newNode.data = self.format_text(help_text)
# insert below this node
if remaining_path:
newNode.insert_node(remaining_path, help_text)
class HelpExplorer(TreeExplorer.TreeExplorer):
"""A widget for viewing a HelpTree.
"""
def createInterior(self):
"Create the components."
interior = self.interior()
self.textDisplay = self.createcomponent('textdisplay', (), None,
Pmw.ScrolledText,
(interior,),
text_state='disabled',
text_wrap='none',
)
self.textDisplay.pack(side=Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.BOTH)
self.panes.configurepane('datapane', min=500, size=500)
return
def select_node(self, node):
"Called when a node is selected."
if not node: return
if node.data:
newText = node.data
else:
newText = 'No help for %s' % string.join(map(lambda x: x.name,
node.getpath()), ':')
try:
self.textDisplay.settext(newText)
except AttributeError:
# not initialized yet
pass
return
|