Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull window resize feature and font size #55

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,23 @@ codeview.pack(fill="both", expand=True)

root.mainloop()
```

#### Resizing CodeView Window
You can dynamically resize the CodeView window by using the resize_window method:

```python
from tkinter import Tk
import pygments.lexers
from chlorophyll import CodeView

root = Tk()

# Create a CodeView instance with a Python lexer
codeview = CodeView(root, lexer=pygments.lexers.PythonLexer, color_scheme="monokai")
codeview.pack(fill="both", expand=True)

# Resize the CodeView widget to a specific width and height
codeview.resize_window(width=800, height=600)

root.mainloop()
```
69 changes: 48 additions & 21 deletions chlorophyll/codeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,56 @@ def __init__(
lexer: LexerType = pygments.lexers.TextLexer,
color_scheme: dict[str, dict[str, str | int]] | str | None = None,
tab_width: int = 4,
font_size :int=11,
linenums_theme: Callable[[], tuple[str, str]] | tuple[str, str] | None = None,
autohide_scrollbar: bool = True,
linenums_border: int = 0,
default_context_menu: bool = False,
show_line_numbers: bool = True,
show_vertical_scrollbar: bool = True,
show_horizontal_scrollbar: bool=True,
**kwargs,
) -> None:
self._frame = ttk.Frame(master)
self._frame.grid_rowconfigure(0, weight=1)
self._frame.grid_columnconfigure(1, weight=1)

self.show_line_numbers = show_line_numbers
self.show_vertical_scrollbar = show_vertical_scrollbar
self.show_horizontal_scrollbar = show_horizontal_scrollbar

kwargs.setdefault("wrap", "none")
kwargs.setdefault("font", ("monospace", 11))
kwargs.setdefault("font", ("monospace", font_size))

linenum_justify = kwargs.pop("justify", "left")

super().__init__(self._frame, **kwargs)
super().grid(row=0, column=1, sticky="nswe")

self._line_numbers = TkLineNumbers(
self._frame,
self,
justify=linenum_justify,
colors=linenums_theme,
borderwidth=kwargs.get("borderwidth", linenums_border),
)
self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview)
self._hs = Scrollbar(
self._frame, autohide=autohide_scrollbar, orient="horizontal", command=self.xview
)

self._line_numbers.grid(row=0, column=0, sticky="ns")
self._vs.grid(row=0, column=2, sticky="ns")
self._hs.grid(row=1, column=1, sticky="we")
self._line_numbers = None # Initialize with None
if self.show_line_numbers: # Conditionally create line numbers
self._line_numbers = TkLineNumbers(
self._frame,
self,
justify=linenum_justify,
colors=linenums_theme,
borderwidth=kwargs.get("borderwidth", linenums_border),
)
self._line_numbers.grid(row=0, column=0, sticky="ns")

self._vs = None # Initialize with None
if self.show_vertical_scrollbar: # Conditionally create vertical scrollbar
self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview)
self._vs.grid(row=0, column=2, sticky="ns")
self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview)
self._vs.grid(row=0, column=2, sticky="ns")

self._hs = None
if self.show_horizontal_scrollbar:
self._hs = Scrollbar(
self._frame, autohide=autohide_scrollbar, orient="horizontal", command=self.xview
)
self._hs.grid(row=1, column=1, sticky="we")

super().configure(
yscrollcommand=self.vertical_scroll,
Expand All @@ -97,7 +114,7 @@ def __init__(
super().bind(f"<{contmand}-a>", self._select_all, add=True)
super().bind(f"<{contmand}-Shift-Z>", self.redo, add=True)
super().bind("<<ContentChanged>>", self.scroll_line_update, add=True)
super().bind("<Button-1>", self._line_numbers.redraw, add=True)
super().bind("<Button-1>", self._line_numbers.redraw, add=True) if self.show_line_numbers else ...

self._orig = f"{self._w}_widget"
self.tk.call("rename", self._w, self._orig)
Expand All @@ -106,7 +123,7 @@ def __init__(
self._set_lexer(lexer)
self._set_color_scheme(color_scheme)

self._line_numbers.redraw()
self._line_numbers.redraw() if self.show_line_numbers else ...

@property
def context_menu(self) -> Menu:
Expand Down Expand Up @@ -324,12 +341,22 @@ def destroy(self) -> None:
BaseWidget.destroy(self._frame)

def horizontal_scroll(self, first: str | float, last: str | float) -> CodeView:
self._hs.set(first, last)
self._hs.set(first, last) if self.show_horizontal_scrollbar else ...

def vertical_scroll(self, first: str | float, last: str | float) -> CodeView:
self._vs.set(first, last)
self._line_numbers.redraw()
self._vs.set(first, last) if self.show_vertical_scrollbar else ...
self._line_numbers.redraw() if self.show_line_numbers else ...

def scroll_line_update(self, event: Event | None = None) -> CodeView:
self.horizontal_scroll(*self.xview())
self.vertical_scroll(*self.yview())

def resize_window(self, width: int, height: int) -> None:
"""Resize the CodeView widget based on the specified width and height in pixels."""
self._frame.config(width=width, height=height)
self.config(width=width, height=height)
self._line_numbers.redraw() if self.show_line_numbers else ... # Update line numbers after resizing

def set_line_spacing(self, spacing: int) -> None:
"""Set the vertical spacing between lines."""
self.configure(spacing3=spacing)