def __init__(self, codeview): opts = dict( height=1, width=1, relief=tk.RAISED, background="#DCEDF2", borderwidth=1, highlightthickness=0, padx=7, pady=7, wrap=tk.NONE, font="EditorFont", ) opts.update(get_syntax_options_for_tag("expression_box")) tk.Text.__init__(self, codeview.winfo_toplevel(), **opts) self._codeview = codeview self._last_focus = None self._last_root_expression = None self.tag_configure("value", get_syntax_options_for_tag("value")) self.tag_configure("before", get_syntax_options_for_tag("active_focus")) self.tag_configure("after", get_syntax_options_for_tag("completed_focus")) self.tag_configure("exception", get_syntax_options_for_tag("exception_focus")) self.tag_raise("exception", "before") self.tag_raise("exception", "after")
def __init__(self, master): super().__init__( master, borderwidth=0, relief="solid", undo=False, read_only=True, font="TkDefaultFont", text_class=SyntaxText, foreground=get_syntax_options_for_tag("stderr")["foreground"], highlightthickness=0, padx=5, pady=5, wrap="char", horizontal_scrollbar=False, ) self.text.tag_configure("hyperlink", **get_syntax_options_for_tag("hyperlink")) self.text.tag_bind("hyperlink", "<Enter>", self._hyperlink_enter) self.text.tag_bind("hyperlink", "<Leave>", self._hyperlink_leave) get_workbench().bind("ToplevelResponse", self._on_toplevel_response, True) self._prev_exception = None self._show_description()
def _update_shell_appearance(self): background = get_syntax_options_for_tag("TEXT")["background"] stderr_fg = get_syntax_options_for_tag("stderr")["foreground"] stdin_fg = get_syntax_options_for_tag("stdin")["foreground"] stdout_fg = get_syntax_options_for_tag("stdout")["foreground"] self._shell_preview.configure(background=background) self._shell_preview.tag_configure("stderr", foreground=stderr_fg) self._shell_preview.tag_configure("stdin", foreground=stdin_fg) self._shell_preview.tag_configure("stdout", foreground=stdout_fg)
def _update_theme(self, event=None): super()._update_theme(event) comment_opts = get_syntax_options_for_tag("comment") gutter_opts = get_syntax_options_for_tag("GUTTER") text_opts = get_syntax_options_for_tag("TEXT") self.text["background"] = gutter_opts["background"] self.text["foreground"] = text_opts["foreground"] self.text.tag_configure("prose", font="TkDefaultFont") self.text.tag_configure("active", font="BoldTkDefaultFont") self.text.tag_configure("annotation", **comment_opts) self.text.tag_configure("default", **comment_opts) self.text.tag_configure("marker", **comment_opts)
def __init__(self, codeview, text): self.text = text self._codeview = codeview self._last_focus = None self._last_root_expression = None self.text.tag_configure("value", get_syntax_options_for_tag("value")) self.text.tag_configure("before", get_syntax_options_for_tag("active_focus")) self.text.tag_configure("after", get_syntax_options_for_tag("completed_focus")) self.text.tag_configure("exception", get_syntax_options_for_tag("exception_focus")) self.text.tag_raise("exception", "before") self.text.tag_raise("exception", "after")
def _show_description(self): self.text.configure( foreground=get_syntax_options_for_tag("TEXT")["foreground"]) self.text.direct_insert( "end", "If last command raised an exception then this view will show the stacktrace." )
def __init__(self, master, renderer_class, link_and_form_handler, image_requester, read_only=False, **kw): text_options = get_syntax_options_for_tag("TEXT") super().__init__( master=master, read_only=read_only, **{ "font": "TkDefaultFont", "background": text_options["background"], "foreground": text_options["foreground"], # "cursor" : "", **kw, }) self._renderer_class = renderer_class self._link_and_form_handler = link_and_form_handler self._image_requester = image_requester self._configure_tags() self._reset_renderer()
def _reconfigure_tags(self): for tag in ["active_focus", "exception_focus"]: conf = get_syntax_options_for_tag(tag).copy() if self._line_debug: # meaning data comes from line-debug conf["borderwidth"] = 0 self._text.tag_configure(tag, **conf)
def get_text_options(self): opts = dict( height=1, width=1, relief=tk.RAISED, background="#DCEDF2", borderwidth=1, highlightthickness=0, padx=7, pady=7, wrap=tk.NONE, font="EditorFont", ) opts.update(get_syntax_options_for_tag("expression_box")) return opts
def set_exception(self, exception_lines_with_frame_info): if exception_lines_with_frame_info == self._prev_exception: return self.text.direct_delete("1.0", "end") if exception_lines_with_frame_info is None: self._show_description() return self.text.configure( foreground=get_syntax_options_for_tag("stderr")["foreground"] ) for line, frame_id, filename, lineno in exception_lines_with_frame_info: if frame_id is not None: frame_tag = "frame_%d" % frame_id def handle_frame_click( event, frame_id=frame_id, filename=filename, lineno=lineno ): get_runner().send_command( InlineCommand("get_frame_info", frame_id=frame_id) ) if os.path.exists(filename): get_workbench().get_editor_notebook().show_file( filename, lineno, set_focus=False ) self.text.tag_bind(frame_tag, "<1>", handle_frame_click, True) start = max(line.find("File"), 0) end = line.replace("\r", "").find("\n") if end < 10: end = len(line) self.text.direct_insert("end", line[:start]) self.text.direct_insert( "end", line[start:end], ("hyperlink", frame_tag) ) self.text.direct_insert("end", line[end:]) else: self.text.direct_insert("end", line) self._prev_exception = exception_lines_with_frame_info
def configure_tags(self): main_font = tk.font.nametofont("TkDefaultFont") bold_font = main_font.copy() bold_font.configure(weight="bold", size=main_font.cget("size")) italic_font = main_font.copy() italic_font.configure(slant="italic", size=main_font.cget("size")) h1_font = main_font.copy() h1_font.configure(size=main_font.cget("size") * 2, weight="bold") h2_font = main_font.copy() h2_font.configure(size=round(main_font.cget("size") * 1.5), weight="bold") h3_font = main_font.copy() h3_font.configure(size=main_font.cget("size"), weight="bold") small_font = main_font.copy() small_font.configure(size=round(main_font.cget("size") * 0.8)) small_italic_font = italic_font.copy() small_italic_font.configure(size=round(main_font.cget("size") * 0.8)) # Underline on font looks better than underline on tag underline_font = main_font.copy() underline_font.configure(underline=True) self.tag_configure("h1", font=h1_font, spacing3=5) self.tag_configure("h2", font=h2_font, spacing3=5) self.tag_configure("h3", font=h3_font, spacing3=5) self.tag_configure("p", spacing1=0, spacing3=10, spacing2=0) self.tag_configure("line_block", spacing1=0, spacing3=10, spacing2=0) self.tag_configure("em", font=italic_font) self.tag_configure("strong", font=bold_font) # TODO: hyperlink syntax options may require different background as well self.tag_configure( "a", **{ **get_syntax_options_for_tag("hyperlink"), "underline": False }, font=underline_font) self.tag_configure("small", font=small_font) self.tag_configure("light", foreground="gray") self.tag_configure("remark", font=small_italic_font) self.tag_bind("a", "<Enter>", self._hyperlink_enter) self.tag_bind("a", "<Leave>", self._hyperlink_leave) self.tag_configure("topic_title", lmargin2=16, font=bold_font) self.tag_configure("topic_body", lmargin1=16, lmargin2=16) self.tag_configure( "code", font="TkFixedFont", # wrap="none", # TODO: needs automatic hor-scrollbar and better padding mgmt # background="#eeeeee" ) # if ui_utils.get_tk_version_info() >= (8,6,6): # self.tag_configure("code", lmargincolor=self["background"]) for i in range(1, 6): self.tag_configure("list%d" % i, lmargin1=i * 10, lmargin2=i * 10 + 10) toti_code_font = bold_font.copy() toti_code_font.configure( family=tk.font.nametofont("TkFixedFont").cget("family"), size=bold_font.cget("size")) self.tag_configure("topic_title_code", font=toti_code_font) self.tag_raise("topic_title_code", "code") self.tag_raise("topic_title_code", "topic_title") self.tag_raise("a", "topic_title") # TODO: topic_title + em self.tag_raise("em", "topic_title") self.tag_raise("a", "em") self.tag_raise("a", "topic_body") self.tag_raise("a", "topic_title") if ui_utils.get_tk_version_info() >= (8, 6, 6): self.tag_configure("sel", lmargincolor=self["background"]) self.tag_raise("sel")
def _update_theme(self, event=None): gutter_opts = get_syntax_options_for_tag("GUTTER") text_opts = get_syntax_options_for_tag("TEXT") self._listbox["background"] = gutter_opts["background"] self._listbox["foreground"] = text_opts["foreground"]
def configure_text(text): spacing1 = 2 spacing3 = 3 text_font = text["font"] text.configure(spacing1=spacing1, spacing3=spacing3) text.master._gutter.configure(spacing1=spacing1, spacing3=spacing3) if isinstance(text_font, str): text_font = font.nametofont(text_font) indent_width = text_font.measure(" ") bbox = text.bbox("1.0") if bbox is None or bbox[3] < 5: # text not ready yet # TODO: Text in Tk 8.6 has sync method return False line_height = bbox[3] + spacing1 + spacing3 print(indent_width, line_height) def ver(x: int, y: int, top: bool, bottom: bool) -> bool: # tells where to show pixels in vertical border of the statement # It would be convenient if tiling started from the start of # 1st char, but it is offset a bit # In order to make computation easier, I'm offsetting x as well x = (x - 5) % indent_width stripe_width = 8 gap = 3 left = indent_width - stripe_width - gap return (left <= x < left + stripe_width or top and y == 0 and x >= left or bottom and y == line_height - 1 and x >= left) def hor(x: int, y: int, top: bool, bottom: bool) -> bool: # tells where to show pixels in statement line return top and y == 0 or bottom and y == line_height - 1 color = get_syntax_options_for_tag("GUTTER").get("background", "gray") for orient, base_predicate in [("hor", hor), ("ver", ver)]: for top in [False, True]: for bottom in [False, True]: def predicate( x, y, # need to make base_predicate, top and bottom local base_predicate=base_predicate, top=top, bottom=bottom, ): return base_predicate(x, y, top, bottom) tag_name = "%s_%s_%s" % (orient, top, bottom) bitmap_path = create_bitmap_file(indent_width, line_height, predicate, tag_name) text.tag_configure(tag_name, background=color, bgstipple="@" + bitmap_path) return True
def __init__(self, master, cnf={}, **kw): super().__init__(master, cnf, **kw) self.bindtags(self.bindtags() + ("ShellText", )) self._before_io = True self._command_history = ( [] ) # actually not really history, because each command occurs only once self._command_history_current_index = None self.bind("<Up>", self._arrow_up, True) self.bind("<Down>", self._arrow_down, True) self.bind("<KeyPress>", self._text_key_press, True) self.bind("<KeyRelease>", self._text_key_release, True) prompt_font = tk.font.nametofont("BoldEditorFont") vert_spacing = 10 io_indent = 16 code_indent = prompt_font.measure(">>> ") self.tag_configure("command", lmargin1=code_indent, lmargin2=code_indent) self.tag_configure( "io", lmargin1=io_indent, lmargin2=io_indent, rmargin=io_indent, font="IOFont", ) if ui_utils.get_tk_version_info() >= (8, 6, 6): self.tag_configure( "io", lmargincolor=get_syntax_options_for_tag("TEXT")["background"]) self.tag_bind("hyperlink", "<ButtonRelease-1>", self._handle_hyperlink) self.tag_bind("hyperlink", "<Enter>", self._hyperlink_enter) self.tag_bind("hyperlink", "<Leave>", self._hyperlink_leave) self.tag_raise("hyperlink") self.tag_configure("vertically_spaced", spacing1=vert_spacing) # Underline on font looks better than underline on tag io_hyperlink_font = tk.font.nametofont("IOFont").copy() io_hyperlink_font.configure(underline=get_syntax_options_for_tag( "hyperlink").get("underline", True)) self.tag_configure("io_hyperlink", underline=False, font=io_hyperlink_font) self.tag_raise("io_hyperlink", "hyperlink") self.tag_configure("suppressed_io", elide=True) # create 3 marks: input_start shows the place where user entered but not-yet-submitted # input starts, output_end shows the end of last output, # output_insert shows where next incoming program output should be inserted self.mark_set("input_start", "end-1c") self.mark_gravity("input_start", tk.LEFT) self.mark_set("output_end", "end-1c") self.mark_gravity("output_end", tk.LEFT) self.mark_set("output_insert", "end-1c") self.mark_gravity("output_insert", tk.RIGHT) self.active_object_tags = set() self._last_welcome_text = None get_workbench().bind("InputRequest", self._handle_input_request, True) get_workbench().bind("ProgramOutput", self._handle_program_output, True) get_workbench().bind("ToplevelResponse", self._handle_toplevel_response, True) get_workbench().bind("DebuggerResponse", self._handle_fancy_debugger_progress, True) self._menu = ShellMenu(self)
def _configure_tags(self): main_font = tkfont.nametofont("TkDefaultFont") x_padding = main_font.measure("m") bold_font = main_font.copy() bold_font.configure(weight="bold", size=main_font.cget("size")) italic_font = main_font.copy() italic_font.configure(slant="italic", size=main_font.cget("size")) h1_font = main_font.copy() h1_font.configure(size=round(main_font.cget("size") * 1.4), weight="bold") h2_font = main_font.copy() h2_font.configure(size=round(main_font.cget("size") * 1.3), weight="bold") h3_font = main_font.copy() h3_font.configure(size=main_font.cget("size"), weight="bold") small_font = main_font.copy() small_font.configure(size=round(main_font.cget("size") * 0.8)) small_italic_font = italic_font.copy() small_italic_font.configure(size=round(main_font.cget("size") * 0.8)) # Underline on font looks better than underline on tag underline_font = main_font.copy() underline_font.configure(underline=True) fixed_font = tkfont.nametofont("TkFixedFont") fixed_bold_font = fixed_font.copy() fixed_bold_font.configure(weight="bold", size=fixed_font.cget("size")) self.tag_configure("_base_", lmargin1=x_padding, lmargin2=x_padding, rmargin=x_padding) self.tag_configure("h1", font=h1_font, spacing3=5) self.tag_configure("h2", font=h2_font, spacing3=5) self.tag_configure("h3", font=h3_font, spacing3=5) # self.tag_configure("p", spacing1=0, spacing3=10, spacing2=0) self.tag_configure("line_block", spacing1=0, spacing3=10, spacing2=0) self.tag_configure("em", font=italic_font) self.tag_configure("strong", font=bold_font) self.tag_configure("hr", wrap="none") self.tag_configure( "table", wrap="none", font=fixed_font ) # Fixed font as otherwise centering later won't work. self.tag_configure( "a", **{ **get_syntax_options_for_tag("hyperlink"), "underline": False }, font=underline_font) self.tag_configure("small", font=small_font) self.tag_configure("light", foreground="gray") self.tag_configure("remark", font=small_italic_font) self.tag_bind("a", "<ButtonRelease-1>", self._hyperlink_click) self.tag_bind("a", "<Enter>", self._hyperlink_enter) self.tag_bind("a", "<Leave>", self._hyperlink_leave) gutter_options = get_syntax_options_for_tag("GUTTER") self.tag_configure( "code", font=fixed_font, background=gutter_options["background"], lmargincolor=self["background"], rmargincolor=self["background"], ) self.tag_configure( "pre", font=fixed_font, wrap= "none", # TODO: needs automatic hor-scrollbar and better padding mgmt background=gutter_options["background"], rmargincolor=self["background"], lmargincolor=self["background"]) # if ui_utils.get_tk_version_info() >= (8,6,6): # self.tag_configure("code", lmargincolor=self["background"]) li_indent = main_font.measure("m") li_bullet_width = main_font.measure(get_ul_li_marker(0)) for i in range(1, 6): indent = x_padding + i * li_indent self.tag_configure("list%d" % i, lmargin1=indent, lmargin2=indent + li_bullet_width) self.tag_raise("a", "em") if ui_utils.get_tk_version_info() >= (8, 6, 6): self.tag_configure("sel", lmargincolor=self["background"]) self.tag_raise("sel")