blob: 808a2aefab4f71a3306413cad3abb024a85900fd [file] [log] [blame]
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +00001"""Simple text browser for IDLE
2
Steven M. Gava44d3d1a2001-07-31 06:59:02 +00003"""
Tal Einat604e7b92018-09-25 15:10:14 +03004from tkinter import Toplevel, Text, TclError,\
Tal Einat3221a632019-07-27 19:57:48 +03005 HORIZONTAL, VERTICAL, NS, EW, NSEW, NONE, WORD, SUNKEN
csabella42bc8be2017-06-29 18:42:17 -04006from tkinter.ttk import Frame, Scrollbar, Button
Terry Jan Reedy82c46152016-06-22 04:54:18 -04007from tkinter.messagebox import showerror
Steven M. Gava44d3d1a2001-07-31 06:59:02 +00008
Tal Einat3221a632019-07-27 19:57:48 +03009from functools import update_wrapper
Tal Einatc87d9f42018-09-23 15:23:15 +030010from idlelib.colorizer import color_config
11
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040012
Tal Einat3221a632019-07-27 19:57:48 +030013class AutoHideScrollbar(Scrollbar):
Tal Einat604e7b92018-09-25 15:10:14 +030014 """A scrollbar that is automatically hidden when not needed.
15
16 Only the grid geometry manager is supported.
17 """
18 def set(self, lo, hi):
19 if float(lo) > 0.0 or float(hi) < 1.0:
20 self.grid()
21 else:
22 self.grid_remove()
23 super().set(lo, hi)
24
25 def pack(self, **kwargs):
26 raise TclError(f'{self.__class__.__name__} does not support "pack"')
27
28 def place(self, **kwargs):
29 raise TclError(f'{self.__class__.__name__} does not support "place"')
30
31
Tal Einat3221a632019-07-27 19:57:48 +030032class ScrollableTextFrame(Frame):
33 """Display text with scrollbar(s)."""
csabella42bc8be2017-06-29 18:42:17 -040034
Tal Einat3221a632019-07-27 19:57:48 +030035 def __init__(self, master, wrap=NONE, **kwargs):
csabella42bc8be2017-06-29 18:42:17 -040036 """Create a frame for Textview.
37
Tal Einat3221a632019-07-27 19:57:48 +030038 master - master widget for this frame
39 wrap - type of text wrapping to use ('word', 'char' or 'none')
csabella42bc8be2017-06-29 18:42:17 -040040
Tal Einat3221a632019-07-27 19:57:48 +030041 All parameters except for 'wrap' are passed to Frame.__init__().
42
43 The Text widget is accessible via the 'text' attribute.
44
45 Note: Changing the wrapping mode of the text widget after
46 instantiation is not supported.
47 """
48 super().__init__(master, **kwargs)
49
50 text = self.text = Text(self, wrap=wrap)
51 text.grid(row=0, column=0, sticky=NSEW)
Tal Einat604e7b92018-09-25 15:10:14 +030052 self.grid_rowconfigure(0, weight=1)
53 self.grid_columnconfigure(0, weight=1)
csabella42bc8be2017-06-29 18:42:17 -040054
Tal Einat604e7b92018-09-25 15:10:14 +030055 # vertical scrollbar
Tal Einat3221a632019-07-27 19:57:48 +030056 self.yscroll = AutoHideScrollbar(self, orient=VERTICAL,
57 takefocus=False,
58 command=text.yview)
59 self.yscroll.grid(row=0, column=1, sticky=NS)
60 text['yscrollcommand'] = self.yscroll.set
Tal Einat604e7b92018-09-25 15:10:14 +030061
Tal Einat3221a632019-07-27 19:57:48 +030062 # horizontal scrollbar - only when wrap is set to NONE
63 if wrap == NONE:
64 self.xscroll = AutoHideScrollbar(self, orient=HORIZONTAL,
65 takefocus=False,
66 command=text.xview)
67 self.xscroll.grid(row=1, column=0, sticky=EW)
68 text['xscrollcommand'] = self.xscroll.set
69 else:
70 self.xscroll = None
csabella42bc8be2017-06-29 18:42:17 -040071
72
73class ViewFrame(Frame):
74 "Display TextFrame and Close button."
Tal Einat3221a632019-07-27 19:57:48 +030075 def __init__(self, parent, contents, wrap='word'):
76 """Create a frame for viewing text with a "Close" button.
77
78 parent - parent widget for this frame
79 contents - text to display
80 wrap - type of text wrapping to use ('word', 'char' or 'none')
81
82 The Text widget is accessible via the 'text' attribute.
83 """
csabella42bc8be2017-06-29 18:42:17 -040084 super().__init__(parent)
85 self.parent = parent
86 self.bind('<Return>', self.ok)
87 self.bind('<Escape>', self.ok)
Tal Einat3221a632019-07-27 19:57:48 +030088 self.textframe = ScrollableTextFrame(self, relief=SUNKEN, height=700)
89
90 text = self.text = self.textframe.text
91 text.insert('1.0', contents)
92 text.configure(wrap=wrap, highlightthickness=0, state='disabled')
93 color_config(text)
94 text.focus_set()
95
csabella42bc8be2017-06-29 18:42:17 -040096 self.button_ok = button_ok = Button(
97 self, text='Close', command=self.ok, takefocus=False)
98 self.textframe.pack(side='top', expand=True, fill='both')
99 button_ok.pack(side='bottom')
100
101 def ok(self, event=None):
102 """Dismiss text viewer dialog."""
103 self.parent.destroy()
104
105
106class ViewWindow(Toplevel):
Terry Jan Reedya3623c82016-08-31 19:45:39 -0400107 "A simple text viewer dialog for IDLE."
Kurt B. Kaiser09cb74b2003-06-12 04:20:56 +0000108
Tal Einat3221a632019-07-27 19:57:48 +0300109 def __init__(self, parent, title, contents, modal=True, wrap=WORD,
Terry Jan Reedybfebfd82017-09-30 17:37:53 -0400110 *, _htest=False, _utest=False):
Louie Luba365da2017-05-18 05:51:31 +0800111 """Show the given text in a scrollable window with a 'close' button.
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000112
Louie Luba365da2017-05-18 05:51:31 +0800113 If modal is left True, users cannot interact with other windows
114 until the textview window is closed.
Terry Jan Reedy537e2c82014-06-05 03:38:34 -0400115
csabella0aa0a062017-05-28 06:50:55 -0400116 parent - parent of this dialog
117 title - string which is title of popup dialog
Tal Einat3221a632019-07-27 19:57:48 +0300118 contents - text to display in dialog
Tal Einat604e7b92018-09-25 15:10:14 +0300119 wrap - type of text wrapping to use ('word', 'char' or 'none')
Terry Jan Reedy537e2c82014-06-05 03:38:34 -0400120 _htest - bool; change box location when running htest.
Louie Luba365da2017-05-18 05:51:31 +0800121 _utest - bool; don't wait_window when running unittest.
Steven M. Gavad721c482001-07-31 10:46:53 +0000122 """
csabella42bc8be2017-06-29 18:42:17 -0400123 super().__init__(parent)
124 self['borderwidth'] = 5
Terry Jan Reedya3623c82016-08-31 19:45:39 -0400125 # Place dialog below parent if running htest.
csabella42bc8be2017-06-29 18:42:17 -0400126 x = parent.winfo_rootx() + 10
127 y = parent.winfo_rooty() + (10 if not _htest else 100)
128 self.geometry(f'=750x500+{x}+{y}')
Steven M. Gava44d3d1a2001-07-31 06:59:02 +0000129
Steven M. Gavad721c482001-07-31 10:46:53 +0000130 self.title(title)
Tal Einat3221a632019-07-27 19:57:48 +0300131 self.viewframe = ViewFrame(self, contents, wrap=wrap)
csabella0aa0a062017-05-28 06:50:55 -0400132 self.protocol("WM_DELETE_WINDOW", self.ok)
csabella42bc8be2017-06-29 18:42:17 -0400133 self.button_ok = button_ok = Button(self, text='Close',
134 command=self.ok, takefocus=False)
135 self.viewframe.pack(side='top', expand=True, fill='both')
Terry Jan Reedye91e7632012-02-05 15:14:20 -0500136
Tal Einatdd743692018-08-02 10:30:06 +0300137 self.is_modal = modal
138 if self.is_modal:
Terry Jan Reedye91e7632012-02-05 15:14:20 -0500139 self.transient(parent)
140 self.grab_set()
Louie Luba365da2017-05-18 05:51:31 +0800141 if not _utest:
142 self.wait_window()
Kurt B. Kaiser6655e4b2002-12-31 16:03:23 +0000143
csabella0aa0a062017-05-28 06:50:55 -0400144 def ok(self, event=None):
145 """Dismiss text viewer dialog."""
Tal Einatdd743692018-08-02 10:30:06 +0300146 if self.is_modal:
147 self.grab_release()
Steven M. Gavad721c482001-07-31 10:46:53 +0000148 self.destroy()
Steven M. Gava44d3d1a2001-07-31 06:59:02 +0000149
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000150
Tal Einat3221a632019-07-27 19:57:48 +0300151def view_text(parent, title, contents, modal=True, wrap='word', _utest=False):
csabella42bc8be2017-06-29 18:42:17 -0400152 """Create text viewer for given text.
csabella0aa0a062017-05-28 06:50:55 -0400153
154 parent - parent of this dialog
155 title - string which is the title of popup dialog
Tal Einat3221a632019-07-27 19:57:48 +0300156 contents - text to display in this dialog
Tal Einat604e7b92018-09-25 15:10:14 +0300157 wrap - type of text wrapping to use ('word', 'char' or 'none')
csabella0aa0a062017-05-28 06:50:55 -0400158 modal - controls if users can interact with other windows while this
159 dialog is displayed
160 _utest - bool; controls wait_window on unittest
161 """
Tal Einat3221a632019-07-27 19:57:48 +0300162 return ViewWindow(parent, title, contents, modal, wrap=wrap, _utest=_utest)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000163
csabella0aa0a062017-05-28 06:50:55 -0400164
Tal Einat604e7b92018-09-25 15:10:14 +0300165def view_file(parent, title, filename, encoding, modal=True, wrap='word',
166 _utest=False):
csabella42bc8be2017-06-29 18:42:17 -0400167 """Create text viewer for text in filename.
csabella0aa0a062017-05-28 06:50:55 -0400168
169 Return error message if file cannot be read. Otherwise calls view_text
170 with contents of the file.
171 """
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000172 try:
Éric Araujoccf03a12011-08-01 17:29:36 +0200173 with open(filename, 'r', encoding=encoding) as file:
174 contents = file.read()
Terry Jan Reedy82c46152016-06-22 04:54:18 -0400175 except OSError:
176 showerror(title='File Load Error',
csabella42bc8be2017-06-29 18:42:17 -0400177 message=f'Unable to load file {filename!r} .',
Terry Jan Reedy82c46152016-06-22 04:54:18 -0400178 parent=parent)
179 except UnicodeDecodeError as err:
180 showerror(title='Unicode Decode Error',
181 message=str(err),
182 parent=parent)
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000183 else:
Tal Einat604e7b92018-09-25 15:10:14 +0300184 return view_text(parent, title, contents, modal, wrap=wrap,
185 _utest=_utest)
terryjreedy295304d2017-05-17 22:59:46 -0400186 return None
Louie Luba365da2017-05-18 05:51:31 +0800187
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000188
Steven M. Gava44d3d1a2001-07-31 06:59:02 +0000189if __name__ == '__main__':
Terry Jan Reedy4d921582018-06-19 19:12:52 -0400190 from unittest import main
191 main('idlelib.idle_test.test_textview', verbosity=2, exit=False)
192
Terry Jan Reedy1b392ff2014-05-24 18:48:18 -0400193 from idlelib.idle_test.htest import run
csabella42bc8be2017-06-29 18:42:17 -0400194 run(ViewWindow)