class ContainedText(AttrElem): """Base class for a text widget contained as a cell in a canvas. Both Captions and Cells are derived from this class. """ def __init__(self, table, parentviewer, attrs): AttrElem.__init__(self, attrs) self._table = table self._container = table.container ## from profile import Profile ## from pstats import Stats ## p = Profile() ## # can't use runcall because that doesn't return the results ## p.runctx('self._viewer = Viewer(master=table.container, context=parentviewer.context, scrolling=0, stylesheet=parentviewer.stylesheet, parent=parentviewer)', ## globals(), locals()) ## Stats(p).strip_dirs().sort_stats('time').print_stats(5) self._viewer = Viewer(master=table.container, context=parentviewer.context, scrolling=0, stylesheet=parentviewer.stylesheet, parent=parentviewer) if not parentviewer.find_parentviewer(): self._viewer.RULE_WIDTH_MAGIC = self._viewer.RULE_WIDTH_MAGIC - 6 # for callback notification self._fw = self._viewer.frame self._tw = self._viewer.text self._tw.config(highlightthickness=0) self._width = 0 self._embedheight = 0 def new_formatter(self): formatter = AbstractFormatter(self._viewer) # set parskip to prevent blank line at top of cell if the content # starts with a <P> or header element. formatter.parskip = 1 return formatter def freeze(self): self._viewer.freeze() def unfreeze(self): self._viewer.unfreeze() def close(self): self._viewer.close() def maxwidth(self): return self._maxwidth # not useful until after finish() def minwidth(self): return self._minwidth # likewise def height(self): return max(self._embedheight, _get_height(self._tw)) def recalc(self): # recalculate width and height upon notification of completion # of all context's readers (usually image readers) min_nonaligned = self._minwidth maxwidth = self._maxwidth embedheight = self._embedheight # take into account all embedded windows for sub in self._viewer.subwindows: # the standard interface is used if the object has a # table_geometry() method if hasattr(sub, 'table_geometry'): submin, submax, height = sub.table_geometry() min_nonaligned = max(min_nonaligned, submin) maxwidth = max(maxwidth, submax) embedheight = max(embedheight, height) else: # this is the best we can do ## print 'non-conformant embedded window:', sub.__class__ ## print 'using generic method, which may be incorrect' geom = sub.winfo_geometry() match = CELLGEOM_RE.search(geom) if match: [w, h, x, y] = map(grailutil.conv_integer, match.group(1, 2, 3, 4)) min_nonaligned = max(min_nonaligned, w) # x+w? maxwidth = max(maxwidth, w) # x+w? embedheight = max(embedheight, h) # y+h? self._embedheight = embedheight self._minwidth = min_nonaligned self._maxwidth = maxwidth return len(self._viewer.subwindows) def finish(self, padding=0): # TBD: if self.layout == AUTOLAYOUT??? self._x = self._y = 0 fw = self._fw tw = self._tw # Set the padding before grabbing the width, but it could be # denoted as a percentage of the viewer width if type(padding) == StringType: try: # divide by 200 since padding is a percentage and we # want to put equal amounts of pad on both sides of # the picture. padding = int(self._table.get_available_width() * string.atoi(padding[:-1]) / 200) except ValueError: padding = 0 tw['padx'] = padding # TBD: according to the W3C table spec, minwidth should really # be max(min_left + min_right, min_nonaligned). Also note # that minwidth is recalculated by minwidth() call self._minwidth, self._maxwidth = _get_widths(self._tw) # first approximation of height. this is the best we can do # without forcing an update_idletasks() fireworks display tw['height'] = _get_linecount(tw) + 1 # initially place the cell in the canvas at position (0,0), # with the maximum width and closest approximation height. # situate() will be called later with the final layout # parameters. self._tag = self._container.create_window( 0, 0, window=fw, anchor=NW, width=self._maxwidth, height=fw['height']) def situate(self, x=0, y=0, width=None, height=None): # canvas.move() deals in relative positioning, but we want # absolute coordinates xdelta = x - self._x ydelta = y - self._y self._x = x self._y = y self._container.move(self._tag, xdelta, ydelta) if width <> None and height <> None: self._container.itemconfigure(self._tag, width=width, height=height) elif width <> None: self._container.itemconfigure(self._tag, width=width) else: self._container.itemconfigure(self._tag, height=height)
class ContainedText(AttrElem): """Base class for a text widget contained as a cell in a canvas. Both Captions and Cells are derived from this class. """ def __init__(self, table, parentviewer, attrs): AttrElem.__init__(self, attrs) self._table = table self._container = table.container ## from profile import Profile ## from pstats import Stats ## p = Profile() ## # can't use runcall because that doesn't return the results ## p.runctx('self._viewer = Viewer(master=table.container, context=parentviewer.context, scrolling=0, stylesheet=parentviewer.stylesheet, parent=parentviewer)', ## globals(), locals()) ## Stats(p).strip_dirs().sort_stats('time').print_stats(5) self._viewer = Viewer(master=table.container, context=parentviewer.context, scrolling=0, stylesheet=parentviewer.stylesheet, parent=parentviewer) if not parentviewer.find_parentviewer(): self._viewer.RULE_WIDTH_MAGIC = self._viewer.RULE_WIDTH_MAGIC - 6 # for callback notification self._fw = self._viewer.frame self._tw = self._viewer.text self._tw.config(highlightthickness=0) self._width = 0 self._embedheight = 0 def new_formatter(self): formatter = AbstractFormatter(self._viewer) # set parskip to prevent blank line at top of cell if the content # starts with a <P> or header element. formatter.parskip = 1 return formatter def freeze(self): self._viewer.freeze() def unfreeze(self): self._viewer.unfreeze() def close(self): self._viewer.close() def maxwidth(self): return self._maxwidth # not useful until after finish() def minwidth(self): return self._minwidth # likewise def height(self): return max(self._embedheight, _get_height(self._tw)) def recalc(self): # recalculate width and height upon notification of completion # of all context's readers (usually image readers) min_nonaligned = self._minwidth maxwidth = self._maxwidth embedheight = self._embedheight # take into account all embedded windows for sub in self._viewer.subwindows: # the standard interface is used if the object has a # table_geometry() method if hasattr(sub, 'table_geometry'): submin, submax, height = sub.table_geometry() min_nonaligned = max(min_nonaligned, submin) maxwidth = max(maxwidth, submax) embedheight = max(embedheight, height) else: # this is the best we can do ## print 'non-conformant embedded window:', sub.__class__ ## print 'using generic method, which may be incorrect' geom = sub.winfo_geometry() if CELLGEOM_RE.search(geom) >= 0: [w, h, x, y] = map(grailutil.conv_integer, CELLGEOM_RE.group(1, 2, 3, 4)) min_nonaligned = max(min_nonaligned, w) # x+w? maxwidth = max(maxwidth, w) # x+w? embedheight = max(embedheight, h) # y+h? self._embedheight = embedheight self._minwidth = min_nonaligned self._maxwidth = maxwidth return len(self._viewer.subwindows) def finish(self, padding=0): # TBD: if self.layout == AUTOLAYOUT??? self._x = self._y = 0 fw = self._fw tw = self._tw # Set the padding before grabbing the width, but it could be # denoted as a percentage of the viewer width if isinstance(padding,str): try: # divide by 200 since padding is a percentage and we # want to put equal amounts of pad on both sides of # the picture. padding = int(self._table.get_available_width() * int(padding[:-1]) / 200) except ValueError: padding = 0 tw['padx'] = padding # TBD: according to the W3C table spec, minwidth should really # be max(min_left + min_right, min_nonaligned). Also note # that minwidth is recalculated by minwidth() call self._minwidth, self._maxwidth = _get_widths(self._tw) # first approximation of height. this is the best we can do # without forcing an update_idletasks() fireworks display tw['height'] = _get_linecount(tw) + 1 # initially place the cell in the canvas at position (0,0), # with the maximum width and closest approximation height. # situate() will be called later with the final layout # parameters. self._tag = self._container.create_window( 0, 0, window=fw, anchor=NW, width=self._maxwidth, height=fw['height']) def situate(self, x=0, y=0, width=None, height=None): # canvas.move() deals in relative positioning, but we want # absolute coordinates xdelta = x - self._x ydelta = y - self._y self._x = x self._y = y self._container.move(self._tag, xdelta, ydelta) if width != None and height != None: self._container.itemconfigure(self._tag, width=width, height=height) elif width != None: self._container.itemconfigure(self._tag, width=width) else: self._container.itemconfigure(self._tag, height=height)