FormattedDocument
(text='')¶Simple implementation of a document that maintains text formatting.
Changes to text style are applied according to the description in
AbstractDocument. All styles default to None
.
Constructor:
__init__
(text='')¶Methods:
delete_text
(start, end)Delete text from the document. get_element
(position)Get the element at a specified position. get_element_runs
()get_font
(position[, dpi])get_font_runs
([dpi])get_paragraph_end
(pos)Get the end position of a paragraph. get_paragraph_start
(pos)Get the starting position of a paragraph. get_style
(attribute[, position])get_style_range
(attribute, start, end)Get an attribute style over the given range. get_style_runs
(attribute)insert_element
(position, element[, attributes])Insert a element into the document. insert_text
(start, text[, attributes])Insert text into the document. set_paragraph_style
(start, end, attributes)Set the style for a range of paragraphs. set_style
(start, end, attributes)Set text style of some or all of the document.
Events:
on_delete_text
(start, end)Text was deleted from the document. on_insert_text
(start, text)Text was inserted into the document. on_style_text
(start, end, attributes)Text character style was modified.
Attributes:
event_types
text
Document text.
FormattedDocument.
get_element_runs
()¶FormattedDocument.
get_font
(position, dpi=None)¶FormattedDocument.
get_font_runs
(dpi=None)¶FormattedDocument.
get_style
(attribute, position=0)¶FormattedDocument.
get_style_runs
(attribute)¶Methods
FormattedDocument.
delete_text
(start, end)Delete text from the document.
Parameters:
- start (int) – Starting character position to delete from.
- end (int) – Ending character position to delete to (exclusive).
FormattedDocument.
dispatch_event
(event_type, *args)Dispatch a single event to the attached handlers.
The event is propagated to all handlers from from the top of the stack until one returns EVENT_HANDLED. This method should be used only by EventDispatcher implementors; applications should call the
dispatch_events
method.Since pyglet 1.2, the method returns EVENT_HANDLED if an event handler returned EVENT_HANDLED or EVENT_UNHANDLED if all events returned EVENT_UNHANDLED. If no matching event handlers are in the stack,
False
is returned.
Parameters:
- event_type (str) – Name of the event.
- args (sequence) – Arguments to pass to the event handler.
Return type: bool or None
Returns: (Since pyglet 1.2) EVENT_HANDLED if an event handler returned EVENT_HANDLED; EVENT_UNHANDLED if one or more event handlers were invoked but returned only EVENT_UNHANDLED; otherwise
False
. In pyglet 1.1 and earler, the return value is alwaysNone
.
FormattedDocument.
event
(*args)Function decorator for an event handler.
Usage:
win = window.Window() @win.event def on_resize(self, width, height): # ...or:
@win.event('on_resize') def foo(self, width, height): # ...
FormattedDocument.
get_element
(position)Get the element at a specified position.
Parameters: position (int) – Position in the document of the element. Return type: InlineElement
FormattedDocument.
get_paragraph_end
(pos)Get the end position of a paragraph.
Parameters: pos (int) – Character position within paragraph. Return type: int
FormattedDocument.
get_paragraph_start
(pos)Get the starting position of a paragraph.
Parameters: pos (int) – Character position within paragraph. Return type: int
FormattedDocument.
get_style_range
(attribute, start, end)Get an attribute style over the given range.
If the style varies over the range, STYLE_INDETERMINATE is returned.
Parameters:
- attribute (str) – Name of style attribute to query.
- start (int) – Starting character position.
- end (int) – Ending character position (exclusive).
Returns: The style set for the attribute over the given range, or STYLE_INDETERMINATE if more than one value is set.
FormattedDocument.
insert_element
(position, element, attributes=None)Insert a element into the document.
See the InlineElement class documentation for details of usage.
Parameters:
- position (int) – Character insertion point within document.
- element (InlineElement) – Element to insert.
- attributes (dict) – Optional dictionary giving named style attributes of the inserted text.
FormattedDocument.
insert_text
(start, text, attributes=None)Insert text into the document.
Parameters:
- start (int) – Character insertion point within document.
- text (str) – Text to insert.
- attributes (dict) – Optional dictionary giving named style attributes of the inserted text.
FormattedDocument.
pop_handlers
()Pop the top level of event handlers off the stack.
FormattedDocument.
push_handlers
(*args, **kwargs)Push a level onto the top of the handler stack, then attach zero or more event handlers.
If keyword arguments are given, they name the event type to attach. Otherwise, a callable’s __name__ attribute will be used. Any other object may also be specified, in which case it will be searched for callables with event names.
FormattedDocument.
register_event_type
(name)Register an event type with the dispatcher.
Registering event types allows the dispatcher to validate event handler names as they are attached, and to search attached objects for suitable handlers.
Parameters: name (str) – Name of the event to register.
FormattedDocument.
remove_handler
(name, handler)Remove a single event handler.
The given event handler is removed from the first handler stack frame it appears in. The handler must be the exact same callable as passed to set_handler, set_handlers or push_handlers; and the name must match the event type it is bound to.
No error is raised if the event handler is not set.
Parameters:
- name (str) – Name of the event type to remove.
- handler (callable) – Event handler to remove.
FormattedDocument.
remove_handlers
(*args, **kwargs)Remove event handlers from the event stack.
See push_handlers for the accepted argument types. All handlers are removed from the first stack frame that contains any of the given handlers. No error is raised if any handler does not appear in that frame, or if no stack frame contains any of the given handlers.
If the stack frame is empty after removing the handlers, it is removed from the stack. Note that this interferes with the expected symmetry of push_handlers and pop_handlers.
FormattedDocument.
set_handler
(name, handler)Attach a single event handler.
Parameters:
- name (str) – Name of the event type to attach to.
- handler (callable) – Event handler to attach.
FormattedDocument.
set_handlers
(*args, **kwargs)Attach one or more event handlers to the top level of the handler stack.
See push_handlers for the accepted argument types.
FormattedDocument.
set_paragraph_style
(start, end, attributes)Set the style for a range of paragraphs.
This is a convenience method for set_style that aligns the character range to the enclosing paragraph(s).
Parameters:
- start (int) – Starting character position.
- end (int) – Ending character position (exclusive).
- attributes (dict) – Dictionary giving named style attributes of the paragraphs.
FormattedDocument.
set_style
(start, end, attributes)Set text style of some or all of the document.
Parameters:
- start (int) – Starting character position.
- end (int) – Ending character position (exclusive).
- attributes (dict) – Dictionary giving named style attributes of the text.
Events
FormattedDocument.
on_delete_text
(start, end)Text was deleted from the document.
Parameters:
- start (int) – Starting character position of deleted text.
- end (int) – Ending character position of deleted text (exclusive).
FormattedDocument.
on_insert_text
(start, text)Text was inserted into the document.
Parameters:
- start (int) – Character insertion point within document.
- text (str) – The text that was inserted.
FormattedDocument.
on_style_text
(start, end, attributes)Text character style was modified.
Parameters:
- start (int) – Starting character position of modified text.
- end (int) – Ending character position of modified text (exclusive).
- attributes (dict) – Dictionary giving updated named style attributes of the text.
Attributes
FormattedDocument.
event_types
= ['on_insert_text', 'on_delete_text', 'on_style_text']
FormattedDocument.
text
Document text.
For efficient incremental updates, use the insert_text and delete_text methods instead of replacing this property.
Type: str