| 1 |
"""IDLE Configuration Dialog: support user customization of IDLE by GUI |
| 2 |
|
| 3 |
Customize font faces, sizes, and colorization attributes. Set indentation |
| 4 |
defaults. Customize keybindings. Colorization and keybindings can be |
| 5 |
saved as user defined sets. Select startup options including shell/editor |
| 6 |
and default window size. Define additional help sources. |
| 7 |
|
| 8 |
Note that tab width in IDLE is currently fixed at eight due to Tk issues. |
| 9 |
Refer to comments in EditorWindow autoindent code for details. |
| 10 |
|
| 11 |
""" |
| 12 |
import re |
| 13 |
|
| 14 |
from tkinter import (Toplevel, Listbox, Canvas, |
| 15 |
StringVar, BooleanVar, IntVar, TRUE, FALSE, |
| 16 |
TOP, BOTTOM, RIGHT, LEFT, SOLID, GROOVE, |
| 17 |
NONE, BOTH, X, Y, W, E, EW, NS, NSEW, NW, |
| 18 |
HORIZONTAL, VERTICAL, ANCHOR, ACTIVE, END, TclError) |
| 19 |
from tkinter.ttk import (Frame, LabelFrame, Button, Checkbutton, Entry, Label, |
| 20 |
OptionMenu, Notebook, Radiobutton, Scrollbar, Style, |
| 21 |
Spinbox, Combobox) |
| 22 |
from tkinter import colorchooser |
| 23 |
import tkinter.font as tkfont |
| 24 |
from tkinter import messagebox |
| 25 |
|
| 26 |
from idlelib.config import idleConf, ConfigChanges |
| 27 |
from idlelib.config_key import GetKeysWindow |
| 28 |
from idlelib.dynoption import DynOptionMenu |
| 29 |
from idlelib import macosx |
| 30 |
from idlelib.query import SectionName, HelpSource |
| 31 |
from idlelib.textview import view_text |
| 32 |
from idlelib.autocomplete import AutoComplete |
| 33 |
from idlelib.codecontext import CodeContext |
| 34 |
from idlelib.parenmatch import ParenMatch |
| 35 |
from idlelib.format import FormatParagraph |
| 36 |
from idlelib.squeezer import Squeezer |
| 37 |
from idlelib.textview import ScrollableTextFrame |
| 38 |
|
| 39 |
changes = ConfigChanges() |
| 40 |
# Reload changed options in the following classes. |
| 41 |
reloadables = (AutoComplete, CodeContext, ParenMatch, FormatParagraph, |
| 42 |
Squeezer) |
| 43 |
|
| 44 |
|
| 45 |
class ConfigDialog(Toplevel): |
| 46 |
"""Config dialog for IDLE. |
| 47 |
""" |
| 48 |
|
| 49 |
def __init__(self, parent, title='', *, _htest=False, _utest=False): |
| 50 |
"""Show the tabbed dialog for user configuration. |
| 51 |
|
| 52 |
Args: |
| 53 |
parent - parent of this dialog |
| 54 |
title - string which is the title of this popup dialog |
| 55 |
_htest - bool, change box location when running htest |
| 56 |
_utest - bool, don't wait_window when running unittest |
| 57 |
|
| 58 |
Note: Focus set on font page fontlist. |
| 59 |
|
| 60 |
Methods: |
| 61 |
create_widgets |
| 62 |
cancel: Bound to DELETE_WINDOW protocol. |
| 63 |
""" |
| 64 |
Toplevel.__init__(self, parent) |
| 65 |
self.parent = parent |
| 66 |
if _htest: |
| 67 |
parent.instance_dict = {} |
| 68 |
if not _utest: |
| 69 |
self.withdraw() |
| 70 |
|
| 71 |
self.title(title or 'IDLE Preferences') |
| 72 |
x = parent.winfo_rootx() + 20 |
| 73 |
y = parent.winfo_rooty() + (30 if not _htest else 150) |
| 74 |
self.geometry(f'+{x}+{y}') |
| 75 |
# Each theme element key is its display name. |
| 76 |
# The first value of the tuple is the sample area tag name. |
| 77 |
# The second value is the display name list sort index. |
| 78 |
self.create_widgets() |
| 79 |
self.resizable(height=FALSE, width=FALSE) |
| 80 |
self.transient(parent) |
| 81 |
self.protocol("WM_DELETE_WINDOW", self.cancel) |
| 82 |
self.fontpage.fontlist.focus_set() |
| 83 |
# XXX Decide whether to keep or delete these key bindings. |
| 84 |
# Key bindings for this dialog. |
| 85 |
# self.bind('<Escape>', self.Cancel) #dismiss dialog, no save |
| 86 |
# self.bind('<Alt-a>', self.Apply) #apply changes, save |
| 87 |
# self.bind('<F1>', self.Help) #context help |
| 88 |
# Attach callbacks after loading config to avoid calling them. |
| 89 |
tracers.attach() |
| 90 |
|
| 91 |
if not _utest: |
| 92 |
self.grab_set() |
| 93 |
self.wm_deiconify() |
| 94 |
self.wait_window() |
| 95 |
|
| 96 |
def create_widgets(self): |
| 97 |
"""Create and place widgets for tabbed dialog. |
| 98 |
|
| 99 |
Widgets Bound to self: |
| 100 |
frame: encloses all other widgets |
| 101 |
note: Notebook |
| 102 |
highpage: HighPage |
| 103 |
fontpage: FontPage |
| 104 |
keyspage: KeysPage |
| 105 |
winpage: WinPage |
| 106 |
shedpage: ShedPage |
| 107 |
extpage: ExtPage |
| 108 |
|
| 109 |
Methods: |
| 110 |
create_action_buttons |
| 111 |
load_configs: Load pages except for extensions. |
| 112 |
activate_config_changes: Tell editors to reload. |
| 113 |
""" |
| 114 |
self.frame = frame = Frame(self, padding=5) |
| 115 |
self.frame.grid(sticky="nwes") |
| 116 |
self.note = note = Notebook(frame) |
| 117 |
self.extpage = ExtPage(note) |
| 118 |
self.highpage = HighPage(note, self.extpage) |
| 119 |
self.fontpage = FontPage(note, self.highpage) |
| 120 |
self.keyspage = KeysPage(note, self.extpage) |
| 121 |
self.winpage = WinPage(note) |
| 122 |
self.shedpage = ShedPage(note) |
| 123 |
|
| 124 |
note.add(self.fontpage, text=' Fonts ') |
| 125 |
note.add(self.highpage, text='Highlights') |
| 126 |
note.add(self.keyspage, text=' Keys ') |
| 127 |
note.add(self.winpage, text=' Windows ') |
| 128 |
note.add(self.shedpage, text=' Shell/Ed ') |
| 129 |
note.add(self.extpage, text='Extensions') |
| 130 |
note.enable_traversal() |
| 131 |
note.pack(side=TOP, expand=TRUE, fill=BOTH) |
| 132 |
self.create_action_buttons().pack(side=BOTTOM) |
| 133 |
|
| 134 |
def create_action_buttons(self): |
| 135 |
"""Return frame of action buttons for dialog. |
| 136 |
|
| 137 |
Methods: |
| 138 |
ok |
| 139 |
apply |
| 140 |
cancel |
| 141 |
help |
| 142 |
|
| 143 |
Widget Structure: |
| 144 |
outer: Frame |
| 145 |
buttons: Frame |
| 146 |
(no assignment): Button (ok) |
| 147 |
(no assignment): Button (apply) |
| 148 |
(no assignment): Button (cancel) |
| 149 |
(no assignment): Button (help) |
| 150 |
(no assignment): Frame |
| 151 |
""" |
| 152 |
if macosx.isAquaTk(): |
| 153 |
# Changing the default padding on OSX results in unreadable |
| 154 |
# text in the buttons. |
| 155 |
padding_args = {} |
| 156 |
else: |
| 157 |
padding_args = {'padding': (6, 3)} |
| 158 |
outer = Frame(self.frame, padding=2) |
| 159 |
buttons_frame = Frame(outer, padding=2) |
| 160 |
self.buttons = {} |
| 161 |
for txt, cmd in ( |
| 162 |
('Ok', self.ok), |
| 163 |
('Apply', self.apply), |
| 164 |
('Cancel', self.cancel), |
| 165 |
('Help', self.help)): |
| 166 |
self.buttons[txt] = Button(buttons_frame, text=txt, command=cmd, |
| 167 |
takefocus=FALSE, **padding_args) |
| 168 |
self.buttons[txt].pack(side=LEFT, padx=5) |
| 169 |
# Add space above buttons. |
| 170 |
Frame(outer, height=2, borderwidth=0).pack(side=TOP) |
| 171 |
buttons_frame.pack(side=BOTTOM) |
| 172 |
return outer |
| 173 |
|
| 174 |
def ok(self): |
| 175 |
"""Apply config changes, then dismiss dialog.""" |
| 176 |
self.apply() |
| 177 |
self.destroy() |
| 178 |
|
| 179 |
def apply(self): |
| 180 |
"""Apply config changes and leave dialog open.""" |
| 181 |
self.deactivate_current_config() |
| 182 |
changes.save_all() |
| 183 |
self.extpage.save_all_changed_extensions() |
| 184 |
self.activate_config_changes() |
| 185 |
|
| 186 |
def cancel(self): |
| 187 |
"""Dismiss config dialog. |
| 188 |
|
| 189 |
Methods: |
| 190 |
destroy: inherited |
| 191 |
""" |
| 192 |
changes.clear() |
| 193 |
self.destroy() |
| 194 |
|
| 195 |
def destroy(self): |
| 196 |
global font_sample_text |
| 197 |
font_sample_text = self.fontpage.font_sample.get('1.0', 'end') |
| 198 |
self.grab_release() |
| 199 |
super().destroy() |
| 200 |
|
| 201 |
def help(self): |
| 202 |
"""Create textview for config dialog help. |
| 203 |
|
| 204 |
Attributes accessed: |
| 205 |
note |
| 206 |
Methods: |
| 207 |
view_text: Method from textview module. |
| 208 |
""" |
| 209 |
page = self.note.tab(self.note.select(), option='text').strip() |
| 210 |
view_text(self, title='Help for IDLE preferences', |
| 211 |
contents=help_common+help_pages.get(page, '')) |
| 212 |
|
| 213 |
def deactivate_current_config(self): |
| 214 |
"""Remove current key bindings in current windows.""" |
| 215 |
for instance in self.parent.instance_dict: |
| 216 |
instance.RemoveKeybindings() |
| 217 |
|
| 218 |
def activate_config_changes(self): |
| 219 |
"""Apply configuration changes to current windows. |
| 220 |
|
| 221 |
Dynamically update the current parent window instances |
| 222 |
with some of the configuration changes. |
| 223 |
""" |
| 224 |
for instance in self.parent.instance_dict: |
| 225 |
instance.ResetColorizer() |
| 226 |
instance.ResetFont() |
| 227 |
instance.set_notabs_indentwidth() |
| 228 |
instance.ApplyKeybindings() |
| 229 |
instance.reset_help_menu_entries() |
| 230 |
instance.update_cursor_blink() |
| 231 |
for klass in reloadables: |
| 232 |
klass.reload() |
| 233 |
|
| 234 |
|
| 235 |
# class TabPage(Frame): # A template for Page classes. |
| 236 |
# def __init__(self, master): |
| 237 |
# super().__init__(master) |
| 238 |
# self.create_page_tab() |
| 239 |
# self.load_tab_cfg() |
| 240 |
# def create_page_tab(self): |
| 241 |
# # Define tk vars and register var and callback with tracers. |
| 242 |
# # Create subframes and widgets. |
| 243 |
# # Pack widgets. |
| 244 |
# def load_tab_cfg(self): |
| 245 |
# # Initialize widgets with data from idleConf. |
| 246 |
# def var_changed_var_name(): |
| 247 |
# # For each tk var that needs other than default callback. |
| 248 |
# def other_methods(): |
| 249 |
# # Define tab-specific behavior. |
| 250 |
|
| 251 |
font_sample_text = ( |
| 252 |
'<ASCII/Latin1>\n' |
| 253 |
'AaBbCcDdEeFfGgHhIiJj\n1234567890#:+=(){}[]\n' |
| 254 |
'\u00a2\u00a3\u00a5\u00a7\u00a9\u00ab\u00ae\u00b6\u00bd\u011e' |
| 255 |
'\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c7\u00d0\u00d8\u00df\n' |
| 256 |
'\n<IPA,Greek,Cyrillic>\n' |
| 257 |
'\u0250\u0255\u0258\u025e\u025f\u0264\u026b\u026e\u0270\u0277' |
| 258 |
'\u027b\u0281\u0283\u0286\u028e\u029e\u02a2\u02ab\u02ad\u02af\n' |
| 259 |
'\u0391\u03b1\u0392\u03b2\u0393\u03b3\u0394\u03b4\u0395\u03b5' |
| 260 |
'\u0396\u03b6\u0397\u03b7\u0398\u03b8\u0399\u03b9\u039a\u03ba\n' |
| 261 |
'\u0411\u0431\u0414\u0434\u0416\u0436\u041f\u043f\u0424\u0444' |
| 262 |
'\u0427\u0447\u042a\u044a\u042d\u044d\u0460\u0464\u046c\u04dc\n' |
| 263 |
'\n<Hebrew, Arabic>\n' |
| 264 |
'\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9' |
| 265 |
'\u05da\u05db\u05dc\u05dd\u05de\u05df\u05e0\u05e1\u05e2\u05e3\n' |
| 266 |
'\u0627\u0628\u062c\u062f\u0647\u0648\u0632\u062d\u0637\u064a' |
| 267 |
'\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669\n' |
| 268 |
'\n<Devanagari, Tamil>\n' |
| 269 |
'\u0966\u0967\u0968\u0969\u096a\u096b\u096c\u096d\u096e\u096f' |
| 270 |
'\u0905\u0906\u0907\u0908\u0909\u090a\u090f\u0910\u0913\u0914\n' |
| 271 |
'\u0be6\u0be7\u0be8\u0be9\u0bea\u0beb\u0bec\u0bed\u0bee\u0bef' |
| 272 |
'\u0b85\u0b87\u0b89\u0b8e\n' |
| 273 |
'\n<East Asian>\n' |
| 274 |
'\u3007\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\n' |
| 275 |
'\u6c49\u5b57\u6f22\u5b57\u4eba\u6728\u706b\u571f\u91d1\u6c34\n' |
| 276 |
'\uac00\ub0d0\ub354\ub824\ubaa8\ubd64\uc218\uc720\uc988\uce58\n' |
| 277 |
'\u3042\u3044\u3046\u3048\u304a\u30a2\u30a4\u30a6\u30a8\u30aa\n' |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
class FontPage(Frame): |
| 282 |
|
| 283 |
def __init__(self, master, highpage): |
| 284 |
super().__init__(master) |
| 285 |
self.highlight_sample = highpage.highlight_sample |
| 286 |
self.create_page_font() |
| 287 |
self.load_font_cfg() |
| 288 |
|
| 289 |
def create_page_font(self): |
| 290 |
"""Return frame of widgets for Font tab. |
| 291 |
|
| 292 |
Fonts: Enable users to provisionally change font face, size, or |
| 293 |
boldness and to see the consequence of proposed choices. Each |
| 294 |
action set 3 options in changes structuree and changes the |
| 295 |
corresponding aspect of the font sample on this page and |
| 296 |
highlight sample on highlight page. |
| 297 |
|
| 298 |
Function load_font_cfg initializes font vars and widgets from |
| 299 |
idleConf entries and tk. |
| 300 |
|
| 301 |
Fontlist: mouse button 1 click or up or down key invoke |
| 302 |
on_fontlist_select(), which sets var font_name. |
| 303 |
|
| 304 |
Sizelist: clicking the menubutton opens the dropdown menu. A |
| 305 |
mouse button 1 click or return key sets var font_size. |
| 306 |
|
| 307 |
Bold_toggle: clicking the box toggles var font_bold. |
| 308 |
|
| 309 |
Changing any of the font vars invokes var_changed_font, which |
| 310 |
adds all 3 font options to changes and calls set_samples. |
| 311 |
Set_samples applies a new font constructed from the font vars to |
| 312 |
font_sample and to highlight_sample on the highlight page. |
| 313 |
|
| 314 |
Widgets for FontPage(Frame): (*) widgets bound to self |
| 315 |
frame_font: LabelFrame |
| 316 |
frame_font_name: Frame |
| 317 |
font_name_title: Label |
| 318 |
(*)fontlist: ListBox - font_name |
| 319 |
scroll_font: Scrollbar |
| 320 |
frame_font_param: Frame |
| 321 |
font_size_title: Label |
| 322 |
(*)sizelist: DynOptionMenu - font_size |
| 323 |
(*)bold_toggle: Checkbutton - font_bold |
| 324 |
frame_sample: LabelFrame |
| 325 |
(*)font_sample: Label |
| 326 |
""" |
| 327 |
self.font_name = tracers.add(StringVar(self), self.var_changed_font) |
| 328 |
self.font_size = tracers.add(StringVar(self), self.var_changed_font) |
| 329 |
self.font_bold = tracers.add(BooleanVar(self), self.var_changed_font) |
| 330 |
|
| 331 |
# Define frames and widgets. |
| 332 |
frame_font = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 333 |
text=' Shell/Editor Font ') |
| 334 |
frame_sample = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 335 |
text=' Font Sample (Editable) ') |
| 336 |
# frame_font. |
| 337 |
frame_font_name = Frame(frame_font) |
| 338 |
frame_font_param = Frame(frame_font) |
| 339 |
font_name_title = Label( |
| 340 |
frame_font_name, justify=LEFT, text='Font Face :') |
| 341 |
self.fontlist = Listbox(frame_font_name, height=15, |
| 342 |
takefocus=True, exportselection=FALSE) |
| 343 |
self.fontlist.bind('<ButtonRelease-1>', self.on_fontlist_select) |
| 344 |
self.fontlist.bind('<KeyRelease-Up>', self.on_fontlist_select) |
| 345 |
self.fontlist.bind('<KeyRelease-Down>', self.on_fontlist_select) |
| 346 |
scroll_font = Scrollbar(frame_font_name) |
| 347 |
scroll_font.config(command=self.fontlist.yview) |
| 348 |
self.fontlist.config(yscrollcommand=scroll_font.set) |
| 349 |
font_size_title = Label(frame_font_param, text='Size :') |
| 350 |
self.sizelist = DynOptionMenu(frame_font_param, self.font_size, None) |
| 351 |
self.bold_toggle = Checkbutton( |
| 352 |
frame_font_param, variable=self.font_bold, |
| 353 |
onvalue=1, offvalue=0, text='Bold') |
| 354 |
# frame_sample. |
| 355 |
font_sample_frame = ScrollableTextFrame(frame_sample) |
| 356 |
self.font_sample = font_sample_frame.text |
| 357 |
self.font_sample.config(wrap=NONE, width=1, height=1) |
| 358 |
self.font_sample.insert(END, font_sample_text) |
| 359 |
|
| 360 |
# Grid and pack widgets: |
| 361 |
self.columnconfigure(1, weight=1) |
| 362 |
self.rowconfigure(2, weight=1) |
| 363 |
frame_font.grid(row=0, column=0, padx=5, pady=5) |
| 364 |
frame_sample.grid(row=0, column=1, rowspan=3, padx=5, pady=5, |
| 365 |
sticky='nsew') |
| 366 |
# frame_font. |
| 367 |
frame_font_name.pack(side=TOP, padx=5, pady=5, fill=X) |
| 368 |
frame_font_param.pack(side=TOP, padx=5, pady=5, fill=X) |
| 369 |
font_name_title.pack(side=TOP, anchor=W) |
| 370 |
self.fontlist.pack(side=LEFT, expand=TRUE, fill=X) |
| 371 |
scroll_font.pack(side=LEFT, fill=Y) |
| 372 |
font_size_title.pack(side=LEFT, anchor=W) |
| 373 |
self.sizelist.pack(side=LEFT, anchor=W) |
| 374 |
self.bold_toggle.pack(side=LEFT, anchor=W, padx=20) |
| 375 |
# frame_sample. |
| 376 |
font_sample_frame.pack(expand=TRUE, fill=BOTH) |
| 377 |
|
| 378 |
def load_font_cfg(self): |
| 379 |
"""Load current configuration settings for the font options. |
| 380 |
|
| 381 |
Retrieve current font with idleConf.GetFont and font families |
| 382 |
from tk. Setup fontlist and set font_name. Setup sizelist, |
| 383 |
which sets font_size. Set font_bold. Call set_samples. |
| 384 |
""" |
| 385 |
configured_font = idleConf.GetFont(self, 'main', 'EditorWindow') |
| 386 |
font_name = configured_font[0].lower() |
| 387 |
font_size = configured_font[1] |
| 388 |
font_bold = configured_font[2]=='bold' |
| 389 |
|
| 390 |
# Set sorted no-duplicate editor font selection list and font_name. |
| 391 |
fonts = sorted(set(tkfont.families(self))) |
| 392 |
for font in fonts: |
| 393 |
self.fontlist.insert(END, font) |
| 394 |
self.font_name.set(font_name) |
| 395 |
lc_fonts = [s.lower() for s in fonts] |
| 396 |
try: |
| 397 |
current_font_index = lc_fonts.index(font_name) |
| 398 |
self.fontlist.see(current_font_index) |
| 399 |
self.fontlist.select_set(current_font_index) |
| 400 |
self.fontlist.select_anchor(current_font_index) |
| 401 |
self.fontlist.activate(current_font_index) |
| 402 |
except ValueError: |
| 403 |
pass |
| 404 |
# Set font size dropdown. |
| 405 |
self.sizelist.SetMenu(('7', '8', '9', '10', '11', '12', '13', '14', |
| 406 |
'16', '18', '20', '22', '25', '29', '34', '40'), |
| 407 |
font_size) |
| 408 |
# Set font weight. |
| 409 |
self.font_bold.set(font_bold) |
| 410 |
self.set_samples() |
| 411 |
|
| 412 |
def var_changed_font(self, *params): |
| 413 |
"""Store changes to font attributes. |
| 414 |
|
| 415 |
When one font attribute changes, save them all, as they are |
| 416 |
not independent from each other. In particular, when we are |
| 417 |
overriding the default font, we need to write out everything. |
| 418 |
""" |
| 419 |
value = self.font_name.get() |
| 420 |
changes.add_option('main', 'EditorWindow', 'font', value) |
| 421 |
value = self.font_size.get() |
| 422 |
changes.add_option('main', 'EditorWindow', 'font-size', value) |
| 423 |
value = self.font_bold.get() |
| 424 |
changes.add_option('main', 'EditorWindow', 'font-bold', value) |
| 425 |
self.set_samples() |
| 426 |
|
| 427 |
def on_fontlist_select(self, event): |
| 428 |
"""Handle selecting a font from the list. |
| 429 |
|
| 430 |
Event can result from either mouse click or Up or Down key. |
| 431 |
Set font_name and example displays to selection. |
| 432 |
""" |
| 433 |
font = self.fontlist.get( |
| 434 |
ACTIVE if event.type.name == 'KeyRelease' else ANCHOR) |
| 435 |
self.font_name.set(font.lower()) |
| 436 |
|
| 437 |
def set_samples(self, event=None): |
| 438 |
"""Update both screen samples with the font settings. |
| 439 |
|
| 440 |
Called on font initialization and change events. |
| 441 |
Accesses font_name, font_size, and font_bold Variables. |
| 442 |
Updates font_sample and highlight page highlight_sample. |
| 443 |
""" |
| 444 |
font_name = self.font_name.get() |
| 445 |
font_weight = tkfont.BOLD if self.font_bold.get() else tkfont.NORMAL |
| 446 |
new_font = (font_name, self.font_size.get(), font_weight) |
| 447 |
self.font_sample['font'] = new_font |
| 448 |
self.highlight_sample['font'] = new_font |
| 449 |
|
| 450 |
|
| 451 |
class HighPage(Frame): |
| 452 |
|
| 453 |
def __init__(self, master, extpage): |
| 454 |
super().__init__(master) |
| 455 |
self.extpage = extpage |
| 456 |
self.cd = master.winfo_toplevel() |
| 457 |
self.style = Style(master) |
| 458 |
self.create_page_highlight() |
| 459 |
self.load_theme_cfg() |
| 460 |
|
| 461 |
def create_page_highlight(self): |
| 462 |
"""Return frame of widgets for Highlights tab. |
| 463 |
|
| 464 |
Enable users to provisionally change foreground and background |
| 465 |
colors applied to textual tags. Color mappings are stored in |
| 466 |
complete listings called themes. Built-in themes in |
| 467 |
idlelib/config-highlight.def are fixed as far as the dialog is |
| 468 |
concerned. Any theme can be used as the base for a new custom |
| 469 |
theme, stored in .idlerc/config-highlight.cfg. |
| 470 |
|
| 471 |
Function load_theme_cfg() initializes tk variables and theme |
| 472 |
lists and calls paint_theme_sample() and set_highlight_target() |
| 473 |
for the current theme. Radiobuttons builtin_theme_on and |
| 474 |
custom_theme_on toggle var theme_source, which controls if the |
| 475 |
current set of colors are from a builtin or custom theme. |
| 476 |
DynOptionMenus builtinlist and customlist contain lists of the |
| 477 |
builtin and custom themes, respectively, and the current item |
| 478 |
from each list is stored in vars builtin_name and custom_name. |
| 479 |
|
| 480 |
Function paint_theme_sample() applies the colors from the theme |
| 481 |
to the tags in text widget highlight_sample and then invokes |
| 482 |
set_color_sample(). Function set_highlight_target() sets the state |
| 483 |
of the radiobuttons fg_on and bg_on based on the tag and it also |
| 484 |
invokes set_color_sample(). |
| 485 |
|
| 486 |
Function set_color_sample() sets the background color for the frame |
| 487 |
holding the color selector. This provides a larger visual of the |
| 488 |
color for the current tag and plane (foreground/background). |
| 489 |
|
| 490 |
Note: set_color_sample() is called from many places and is often |
| 491 |
called more than once when a change is made. It is invoked when |
| 492 |
foreground or background is selected (radiobuttons), from |
| 493 |
paint_theme_sample() (theme is changed or load_cfg is called), and |
| 494 |
from set_highlight_target() (target tag is changed or load_cfg called). |
| 495 |
|
| 496 |
Button delete_custom invokes delete_custom() to delete |
| 497 |
a custom theme from idleConf.userCfg['highlight'] and changes. |
| 498 |
Button save_custom invokes save_as_new_theme() which calls |
| 499 |
get_new_theme_name() and create_new() to save a custom theme |
| 500 |
and its colors to idleConf.userCfg['highlight']. |
| 501 |
|
| 502 |
Radiobuttons fg_on and bg_on toggle var fg_bg_toggle to control |
| 503 |
if the current selected color for a tag is for the foreground or |
| 504 |
background. |
| 505 |
|
| 506 |
DynOptionMenu targetlist contains a readable description of the |
| 507 |
tags applied to Python source within IDLE. Selecting one of the |
| 508 |
tags from this list populates highlight_target, which has a callback |
| 509 |
function set_highlight_target(). |
| 510 |
|
| 511 |
Text widget highlight_sample displays a block of text (which is |
| 512 |
mock Python code) in which is embedded the defined tags and reflects |
| 513 |
the color attributes of the current theme and changes for those tags. |
| 514 |
Mouse button 1 allows for selection of a tag and updates |
| 515 |
highlight_target with that tag value. |
| 516 |
|
| 517 |
Note: The font in highlight_sample is set through the config in |
| 518 |
the fonts tab. |
| 519 |
|
| 520 |
In other words, a tag can be selected either from targetlist or |
| 521 |
by clicking on the sample text within highlight_sample. The |
| 522 |
plane (foreground/background) is selected via the radiobutton. |
| 523 |
Together, these two (tag and plane) control what color is |
| 524 |
shown in set_color_sample() for the current theme. Button set_color |
| 525 |
invokes get_color() which displays a ColorChooser to change the |
| 526 |
color for the selected tag/plane. If a new color is picked, |
| 527 |
it will be saved to changes and the highlight_sample and |
| 528 |
frame background will be updated. |
| 529 |
|
| 530 |
Tk Variables: |
| 531 |
color: Color of selected target. |
| 532 |
builtin_name: Menu variable for built-in theme. |
| 533 |
custom_name: Menu variable for custom theme. |
| 534 |
fg_bg_toggle: Toggle for foreground/background color. |
| 535 |
Note: this has no callback. |
| 536 |
theme_source: Selector for built-in or custom theme. |
| 537 |
highlight_target: Menu variable for the highlight tag target. |
| 538 |
|
| 539 |
Instance Data Attributes: |
| 540 |
theme_elements: Dictionary of tags for text highlighting. |
| 541 |
The key is the display name and the value is a tuple of |
| 542 |
(tag name, display sort order). |
| 543 |
|
| 544 |
Methods [attachment]: |
| 545 |
load_theme_cfg: Load current highlight colors. |
| 546 |
get_color: Invoke colorchooser [button_set_color]. |
| 547 |
set_color_sample_binding: Call set_color_sample [fg_bg_toggle]. |
| 548 |
set_highlight_target: set fg_bg_toggle, set_color_sample(). |
| 549 |
set_color_sample: Set frame background to target. |
| 550 |
on_new_color_set: Set new color and add option. |
| 551 |
paint_theme_sample: Recolor sample. |
| 552 |
get_new_theme_name: Get from popup. |
| 553 |
create_new: Combine theme with changes and save. |
| 554 |
save_as_new_theme: Save [button_save_custom]. |
| 555 |
set_theme_type: Command for [theme_source]. |
| 556 |
delete_custom: Activate default [button_delete_custom]. |
| 557 |
save_new: Save to userCfg['theme'] (is function). |
| 558 |
|
| 559 |
Widgets of highlights page frame: (*) widgets bound to self |
| 560 |
frame_custom: LabelFrame |
| 561 |
(*)highlight_sample: Text |
| 562 |
(*)frame_color_set: Frame |
| 563 |
(*)button_set_color: Button |
| 564 |
(*)targetlist: DynOptionMenu - highlight_target |
| 565 |
frame_fg_bg_toggle: Frame |
| 566 |
(*)fg_on: Radiobutton - fg_bg_toggle |
| 567 |
(*)bg_on: Radiobutton - fg_bg_toggle |
| 568 |
(*)button_save_custom: Button |
| 569 |
frame_theme: LabelFrame |
| 570 |
theme_type_title: Label |
| 571 |
(*)builtin_theme_on: Radiobutton - theme_source |
| 572 |
(*)custom_theme_on: Radiobutton - theme_source |
| 573 |
(*)builtinlist: DynOptionMenu - builtin_name |
| 574 |
(*)customlist: DynOptionMenu - custom_name |
| 575 |
(*)button_delete_custom: Button |
| 576 |
(*)theme_message: Label |
| 577 |
""" |
| 578 |
self.theme_elements = { |
| 579 |
# Display-name: internal-config-tag-name. |
| 580 |
'Normal Code or Text': 'normal', |
| 581 |
'Code Context': 'context', |
| 582 |
'Python Keywords': 'keyword', |
| 583 |
'Python Definitions': 'definition', |
| 584 |
'Python Builtins': 'builtin', |
| 585 |
'Python Comments': 'comment', |
| 586 |
'Python Strings': 'string', |
| 587 |
'Selected Text': 'hilite', |
| 588 |
'Found Text': 'hit', |
| 589 |
'Cursor': 'cursor', |
| 590 |
'Editor Breakpoint': 'break', |
| 591 |
'Shell Prompt': 'console', |
| 592 |
'Error Text': 'error', |
| 593 |
'Shell User Output': 'stdout', |
| 594 |
'Shell User Exception': 'stderr', |
| 595 |
'Line Number': 'linenumber', |
| 596 |
} |
| 597 |
self.builtin_name = tracers.add( |
| 598 |
StringVar(self), self.var_changed_builtin_name) |
| 599 |
self.custom_name = tracers.add( |
| 600 |
StringVar(self), self.var_changed_custom_name) |
| 601 |
self.fg_bg_toggle = BooleanVar(self) |
| 602 |
self.color = tracers.add( |
| 603 |
StringVar(self), self.var_changed_color) |
| 604 |
self.theme_source = tracers.add( |
| 605 |
BooleanVar(self), self.var_changed_theme_source) |
| 606 |
self.highlight_target = tracers.add( |
| 607 |
StringVar(self), self.var_changed_highlight_target) |
| 608 |
|
| 609 |
# Create widgets: |
| 610 |
# body frame and section frames. |
| 611 |
frame_custom = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 612 |
text=' Custom Highlighting ') |
| 613 |
frame_theme = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 614 |
text=' Highlighting Theme ') |
| 615 |
# frame_custom. |
| 616 |
sample_frame = ScrollableTextFrame( |
| 617 |
frame_custom, relief=SOLID, borderwidth=1) |
| 618 |
text = self.highlight_sample = sample_frame.text |
| 619 |
text.configure( |
| 620 |
font=('courier', 12, ''), cursor='hand2', width=1, height=1, |
| 621 |
takefocus=FALSE, highlightthickness=0, wrap=NONE) |
| 622 |
# Prevent perhaps invisible selection of word or slice. |
| 623 |
text.bind('<Double-Button-1>', lambda e: 'break') |
| 624 |
text.bind('<B1-Motion>', lambda e: 'break') |
| 625 |
string_tags=( |
| 626 |
('# Click selects item.', 'comment'), ('\n', 'normal'), |
| 627 |
('code context section', 'context'), ('\n', 'normal'), |
| 628 |
('| cursor', 'cursor'), ('\n', 'normal'), |
| 629 |
('def', 'keyword'), (' ', 'normal'), |
| 630 |
('func', 'definition'), ('(param):\n ', 'normal'), |
| 631 |
('"Return None."', 'string'), ('\n var0 = ', 'normal'), |
| 632 |
("'string'", 'string'), ('\n var1 = ', 'normal'), |
| 633 |
("'selected'", 'hilite'), ('\n var2 = ', 'normal'), |
| 634 |
("'found'", 'hit'), ('\n var3 = ', 'normal'), |
| 635 |
('list', 'builtin'), ('(', 'normal'), |
| 636 |
('None', 'keyword'), (')\n', 'normal'), |
| 637 |
(' breakpoint("line")', 'break'), ('\n\n', 'normal'), |
| 638 |
('>>>', 'console'), (' 3.14**2\n', 'normal'), |
| 639 |
('9.8596', 'stdout'), ('\n', 'normal'), |
| 640 |
('>>>', 'console'), (' pri ', 'normal'), |
| 641 |
('n', 'error'), ('t(\n', 'normal'), |
| 642 |
('SyntaxError', 'stderr'), ('\n', 'normal')) |
| 643 |
for string, tag in string_tags: |
| 644 |
text.insert(END, string, tag) |
| 645 |
n_lines = len(text.get('1.0', END).splitlines()) |
| 646 |
for lineno in range(1, n_lines): |
| 647 |
text.insert(f'{lineno}.0', |
| 648 |
f'{lineno:{len(str(n_lines))}d} ', |
| 649 |
'linenumber') |
| 650 |
for element in self.theme_elements: |
| 651 |
def tem(event, elem=element): |
| 652 |
# event.widget.winfo_top_level().highlight_target.set(elem) |
| 653 |
self.highlight_target.set(elem) |
| 654 |
text.tag_bind( |
| 655 |
self.theme_elements[element], '<ButtonPress-1>', tem) |
| 656 |
text['state'] = 'disabled' |
| 657 |
self.style.configure('frame_color_set.TFrame', borderwidth=1, |
| 658 |
relief='solid') |
| 659 |
self.frame_color_set = Frame(frame_custom, style='frame_color_set.TFrame') |
| 660 |
frame_fg_bg_toggle = Frame(frame_custom) |
| 661 |
self.button_set_color = Button( |
| 662 |
self.frame_color_set, text='Choose Color for :', |
| 663 |
command=self.get_color) |
| 664 |
self.targetlist = DynOptionMenu( |
| 665 |
self.frame_color_set, self.highlight_target, None, |
| 666 |
highlightthickness=0) #, command=self.set_highlight_targetBinding |
| 667 |
self.fg_on = Radiobutton( |
| 668 |
frame_fg_bg_toggle, variable=self.fg_bg_toggle, value=1, |
| 669 |
text='Foreground', command=self.set_color_sample_binding) |
| 670 |
self.bg_on = Radiobutton( |
| 671 |
frame_fg_bg_toggle, variable=self.fg_bg_toggle, value=0, |
| 672 |
text='Background', command=self.set_color_sample_binding) |
| 673 |
self.fg_bg_toggle.set(1) |
| 674 |
self.button_save_custom = Button( |
| 675 |
frame_custom, text='Save as New Custom Theme', |
| 676 |
command=self.save_as_new_theme) |
| 677 |
# frame_theme. |
| 678 |
theme_type_title = Label(frame_theme, text='Select : ') |
| 679 |
self.builtin_theme_on = Radiobutton( |
| 680 |
frame_theme, variable=self.theme_source, value=1, |
| 681 |
command=self.set_theme_type, text='a Built-in Theme') |
| 682 |
self.custom_theme_on = Radiobutton( |
| 683 |
frame_theme, variable=self.theme_source, value=0, |
| 684 |
command=self.set_theme_type, text='a Custom Theme') |
| 685 |
self.builtinlist = DynOptionMenu( |
| 686 |
frame_theme, self.builtin_name, None, command=None) |
| 687 |
self.customlist = DynOptionMenu( |
| 688 |
frame_theme, self.custom_name, None, command=None) |
| 689 |
self.button_delete_custom = Button( |
| 690 |
frame_theme, text='Delete Custom Theme', |
| 691 |
command=self.delete_custom) |
| 692 |
self.theme_message = Label(frame_theme, borderwidth=2) |
| 693 |
# Pack widgets: |
| 694 |
# body. |
| 695 |
frame_custom.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH) |
| 696 |
frame_theme.pack(side=TOP, padx=5, pady=5, fill=X) |
| 697 |
# frame_custom. |
| 698 |
self.frame_color_set.pack(side=TOP, padx=5, pady=5, fill=X) |
| 699 |
frame_fg_bg_toggle.pack(side=TOP, padx=5, pady=0) |
| 700 |
sample_frame.pack( |
| 701 |
side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) |
| 702 |
self.button_set_color.pack(side=TOP, expand=TRUE, fill=X, padx=8, pady=4) |
| 703 |
self.targetlist.pack(side=TOP, expand=TRUE, fill=X, padx=8, pady=3) |
| 704 |
self.fg_on.pack(side=LEFT, anchor=E) |
| 705 |
self.bg_on.pack(side=RIGHT, anchor=W) |
| 706 |
self.button_save_custom.pack(side=BOTTOM, fill=X, padx=5, pady=5) |
| 707 |
# frame_theme. |
| 708 |
theme_type_title.pack(side=TOP, anchor=W, padx=5, pady=5) |
| 709 |
self.builtin_theme_on.pack(side=TOP, anchor=W, padx=5) |
| 710 |
self.custom_theme_on.pack(side=TOP, anchor=W, padx=5, pady=2) |
| 711 |
self.builtinlist.pack(side=TOP, fill=X, padx=5, pady=5) |
| 712 |
self.customlist.pack(side=TOP, fill=X, anchor=W, padx=5, pady=5) |
| 713 |
self.button_delete_custom.pack(side=TOP, fill=X, padx=5, pady=5) |
| 714 |
self.theme_message.pack(side=TOP, fill=X, pady=5) |
| 715 |
|
| 716 |
def load_theme_cfg(self): |
| 717 |
"""Load current configuration settings for the theme options. |
| 718 |
|
| 719 |
Based on the theme_source toggle, the theme is set as |
| 720 |
either builtin or custom and the initial widget values |
| 721 |
reflect the current settings from idleConf. |
| 722 |
|
| 723 |
Attributes updated: |
| 724 |
theme_source: Set from idleConf. |
| 725 |
builtinlist: List of default themes from idleConf. |
| 726 |
customlist: List of custom themes from idleConf. |
| 727 |
custom_theme_on: Disabled if there are no custom themes. |
| 728 |
custom_theme: Message with additional information. |
| 729 |
targetlist: Create menu from self.theme_elements. |
| 730 |
|
| 731 |
Methods: |
| 732 |
set_theme_type |
| 733 |
paint_theme_sample |
| 734 |
set_highlight_target |
| 735 |
""" |
| 736 |
# Set current theme type radiobutton. |
| 737 |
self.theme_source.set(idleConf.GetOption( |
| 738 |
'main', 'Theme', 'default', type='bool', default=1)) |
| 739 |
# Set current theme. |
| 740 |
current_option = idleConf.CurrentTheme() |
| 741 |
# Load available theme option menus. |
| 742 |
if self.theme_source.get(): # Default theme selected. |
| 743 |
item_list = idleConf.GetSectionList('default', 'highlight') |
| 744 |
item_list.sort() |
| 745 |
self.builtinlist.SetMenu(item_list, current_option) |
| 746 |
item_list = idleConf.GetSectionList('user', 'highlight') |
| 747 |
item_list.sort() |
| 748 |
if not item_list: |
| 749 |
self.custom_theme_on.state(('disabled',)) |
| 750 |
self.custom_name.set('- no custom themes -') |
| 751 |
else: |
| 752 |
self.customlist.SetMenu(item_list, item_list[0]) |
| 753 |
else: # User theme selected. |
| 754 |
item_list = idleConf.GetSectionList('user', 'highlight') |
| 755 |
item_list.sort() |
| 756 |
self.customlist.SetMenu(item_list, current_option) |
| 757 |
item_list = idleConf.GetSectionList('default', 'highlight') |
| 758 |
item_list.sort() |
| 759 |
self.builtinlist.SetMenu(item_list, item_list[0]) |
| 760 |
self.set_theme_type() |
| 761 |
# Load theme element option menu. |
| 762 |
theme_names = list(self.theme_elements) |
| 763 |
self.targetlist.SetMenu(theme_names, theme_names[0]) |
| 764 |
self.paint_theme_sample() |
| 765 |
self.set_highlight_target() |
| 766 |
|
| 767 |
def var_changed_builtin_name(self, *params): |
| 768 |
"""Process new builtin theme selection. |
| 769 |
|
| 770 |
Add the changed theme's name to the changed_items and recreate |
| 771 |
the sample with the values from the selected theme. |
| 772 |
""" |
| 773 |
old_themes = ('IDLE Classic', 'IDLE New') |
| 774 |
value = self.builtin_name.get() |
| 775 |
if value not in old_themes: |
| 776 |
if idleConf.GetOption('main', 'Theme', 'name') not in old_themes: |
| 777 |
changes.add_option('main', 'Theme', 'name', old_themes[0]) |
| 778 |
changes.add_option('main', 'Theme', 'name2', value) |
| 779 |
self.theme_message['text'] = 'New theme, see Help' |
| 780 |
else: |
| 781 |
changes.add_option('main', 'Theme', 'name', value) |
| 782 |
changes.add_option('main', 'Theme', 'name2', '') |
| 783 |
self.theme_message['text'] = '' |
| 784 |
self.paint_theme_sample() |
| 785 |
|
| 786 |
def var_changed_custom_name(self, *params): |
| 787 |
"""Process new custom theme selection. |
| 788 |
|
| 789 |
If a new custom theme is selected, add the name to the |
| 790 |
changed_items and apply the theme to the sample. |
| 791 |
""" |
| 792 |
value = self.custom_name.get() |
| 793 |
if value != '- no custom themes -': |
| 794 |
changes.add_option('main', 'Theme', 'name', value) |
| 795 |
self.paint_theme_sample() |
| 796 |
|
| 797 |
def var_changed_theme_source(self, *params): |
| 798 |
"""Process toggle between builtin and custom theme. |
| 799 |
|
| 800 |
Update the default toggle value and apply the newly |
| 801 |
selected theme type. |
| 802 |
""" |
| 803 |
value = self.theme_source.get() |
| 804 |
changes.add_option('main', 'Theme', 'default', value) |
| 805 |
if value: |
| 806 |
self.var_changed_builtin_name() |
| 807 |
else: |
| 808 |
self.var_changed_custom_name() |
| 809 |
|
| 810 |
def var_changed_color(self, *params): |
| 811 |
"Process change to color choice." |
| 812 |
self.on_new_color_set() |
| 813 |
|
| 814 |
def var_changed_highlight_target(self, *params): |
| 815 |
"Process selection of new target tag for highlighting." |
| 816 |
self.set_highlight_target() |
| 817 |
|
| 818 |
def set_theme_type(self): |
| 819 |
"""Set available screen options based on builtin or custom theme. |
| 820 |
|
| 821 |
Attributes accessed: |
| 822 |
theme_source |
| 823 |
|
| 824 |
Attributes updated: |
| 825 |
builtinlist |
| 826 |
customlist |
| 827 |
button_delete_custom |
| 828 |
custom_theme_on |
| 829 |
|
| 830 |
Called from: |
| 831 |
handler for builtin_theme_on and custom_theme_on |
| 832 |
delete_custom |
| 833 |
create_new |
| 834 |
load_theme_cfg |
| 835 |
""" |
| 836 |
if self.theme_source.get(): |
| 837 |
self.builtinlist['state'] = 'normal' |
| 838 |
self.customlist['state'] = 'disabled' |
| 839 |
self.button_delete_custom.state(('disabled',)) |
| 840 |
else: |
| 841 |
self.builtinlist['state'] = 'disabled' |
| 842 |
self.custom_theme_on.state(('!disabled',)) |
| 843 |
self.customlist['state'] = 'normal' |
| 844 |
self.button_delete_custom.state(('!disabled',)) |
| 845 |
|
| 846 |
def get_color(self): |
| 847 |
"""Handle button to select a new color for the target tag. |
| 848 |
|
| 849 |
If a new color is selected while using a builtin theme, a |
| 850 |
name must be supplied to create a custom theme. |
| 851 |
|
| 852 |
Attributes accessed: |
| 853 |
highlight_target |
| 854 |
frame_color_set |
| 855 |
theme_source |
| 856 |
|
| 857 |
Attributes updated: |
| 858 |
color |
| 859 |
|
| 860 |
Methods: |
| 861 |
get_new_theme_name |
| 862 |
create_new |
| 863 |
""" |
| 864 |
target = self.highlight_target.get() |
| 865 |
prev_color = self.style.lookup(self.frame_color_set['style'], |
| 866 |
'background') |
| 867 |
rgbTuplet, color_string = colorchooser.askcolor( |
| 868 |
parent=self, title='Pick new color for : '+target, |
| 869 |
initialcolor=prev_color) |
| 870 |
if color_string and (color_string != prev_color): |
| 871 |
# User didn't cancel and they chose a new color. |
| 872 |
if self.theme_source.get(): # Current theme is a built-in. |
| 873 |
message = ('Your changes will be saved as a new Custom Theme. ' |
| 874 |
'Enter a name for your new Custom Theme below.') |
| 875 |
new_theme = self.get_new_theme_name(message) |
| 876 |
if not new_theme: # User cancelled custom theme creation. |
| 877 |
return |
| 878 |
else: # Create new custom theme based on previously active theme. |
| 879 |
self.create_new(new_theme) |
| 880 |
self.color.set(color_string) |
| 881 |
else: # Current theme is user defined. |
| 882 |
self.color.set(color_string) |
| 883 |
|
| 884 |
def on_new_color_set(self): |
| 885 |
"Display sample of new color selection on the dialog." |
| 886 |
new_color = self.color.get() |
| 887 |
self.style.configure('frame_color_set.TFrame', background=new_color) |
| 888 |
plane = 'foreground' if self.fg_bg_toggle.get() else 'background' |
| 889 |
sample_element = self.theme_elements[self.highlight_target.get()] |
| 890 |
self.highlight_sample.tag_config(sample_element, **{plane: new_color}) |
| 891 |
theme = self.custom_name.get() |
| 892 |
theme_element = sample_element + '-' + plane |
| 893 |
changes.add_option('highlight', theme, theme_element, new_color) |
| 894 |
|
| 895 |
def get_new_theme_name(self, message): |
| 896 |
"Return name of new theme from query popup." |
| 897 |
used_names = (idleConf.GetSectionList('user', 'highlight') + |
| 898 |
idleConf.GetSectionList('default', 'highlight')) |
| 899 |
new_theme = SectionName( |
| 900 |
self, 'New Custom Theme', message, used_names).result |
| 901 |
return new_theme |
| 902 |
|
| 903 |
def save_as_new_theme(self): |
| 904 |
"""Prompt for new theme name and create the theme. |
| 905 |
|
| 906 |
Methods: |
| 907 |
get_new_theme_name |
| 908 |
create_new |
| 909 |
""" |
| 910 |
new_theme_name = self.get_new_theme_name('New Theme Name:') |
| 911 |
if new_theme_name: |
| 912 |
self.create_new(new_theme_name) |
| 913 |
|
| 914 |
def create_new(self, new_theme_name): |
| 915 |
"""Create a new custom theme with the given name. |
| 916 |
|
| 917 |
Create the new theme based on the previously active theme |
| 918 |
with the current changes applied. Once it is saved, then |
| 919 |
activate the new theme. |
| 920 |
|
| 921 |
Attributes accessed: |
| 922 |
builtin_name |
| 923 |
custom_name |
| 924 |
|
| 925 |
Attributes updated: |
| 926 |
customlist |
| 927 |
theme_source |
| 928 |
|
| 929 |
Method: |
| 930 |
save_new |
| 931 |
set_theme_type |
| 932 |
""" |
| 933 |
if self.theme_source.get(): |
| 934 |
theme_type = 'default' |
| 935 |
theme_name = self.builtin_name.get() |
| 936 |
else: |
| 937 |
theme_type = 'user' |
| 938 |
theme_name = self.custom_name.get() |
| 939 |
new_theme = idleConf.GetThemeDict(theme_type, theme_name) |
| 940 |
# Apply any of the old theme's unsaved changes to the new theme. |
| 941 |
if theme_name in changes['highlight']: |
| 942 |
theme_changes = changes['highlight'][theme_name] |
| 943 |
for element in theme_changes: |
| 944 |
new_theme[element] = theme_changes[element] |
| 945 |
# Save the new theme. |
| 946 |
self.save_new(new_theme_name, new_theme) |
| 947 |
# Change GUI over to the new theme. |
| 948 |
custom_theme_list = idleConf.GetSectionList('user', 'highlight') |
| 949 |
custom_theme_list.sort() |
| 950 |
self.customlist.SetMenu(custom_theme_list, new_theme_name) |
| 951 |
self.theme_source.set(0) |
| 952 |
self.set_theme_type() |
| 953 |
|
| 954 |
def set_highlight_target(self): |
| 955 |
"""Set fg/bg toggle and color based on highlight tag target. |
| 956 |
|
| 957 |
Instance variables accessed: |
| 958 |
highlight_target |
| 959 |
|
| 960 |
Attributes updated: |
| 961 |
fg_on |
| 962 |
bg_on |
| 963 |
fg_bg_toggle |
| 964 |
|
| 965 |
Methods: |
| 966 |
set_color_sample |
| 967 |
|
| 968 |
Called from: |
| 969 |
var_changed_highlight_target |
| 970 |
load_theme_cfg |
| 971 |
""" |
| 972 |
if self.highlight_target.get() == 'Cursor': # bg not possible |
| 973 |
self.fg_on.state(('disabled',)) |
| 974 |
self.bg_on.state(('disabled',)) |
| 975 |
self.fg_bg_toggle.set(1) |
| 976 |
else: # Both fg and bg can be set. |
| 977 |
self.fg_on.state(('!disabled',)) |
| 978 |
self.bg_on.state(('!disabled',)) |
| 979 |
self.fg_bg_toggle.set(1) |
| 980 |
self.set_color_sample() |
| 981 |
|
| 982 |
def set_color_sample_binding(self, *args): |
| 983 |
"""Change color sample based on foreground/background toggle. |
| 984 |
|
| 985 |
Methods: |
| 986 |
set_color_sample |
| 987 |
""" |
| 988 |
self.set_color_sample() |
| 989 |
|
| 990 |
def set_color_sample(self): |
| 991 |
"""Set the color of the frame background to reflect the selected target. |
| 992 |
|
| 993 |
Instance variables accessed: |
| 994 |
theme_elements |
| 995 |
highlight_target |
| 996 |
fg_bg_toggle |
| 997 |
highlight_sample |
| 998 |
|
| 999 |
Attributes updated: |
| 1000 |
frame_color_set |
| 1001 |
""" |
| 1002 |
# Set the color sample area. |
| 1003 |
tag = self.theme_elements[self.highlight_target.get()] |
| 1004 |
plane = 'foreground' if self.fg_bg_toggle.get() else 'background' |
| 1005 |
color = self.highlight_sample.tag_cget(tag, plane) |
| 1006 |
self.style.configure('frame_color_set.TFrame', background=color) |
| 1007 |
|
| 1008 |
def paint_theme_sample(self): |
| 1009 |
"""Apply the theme colors to each element tag in the sample text. |
| 1010 |
|
| 1011 |
Instance attributes accessed: |
| 1012 |
theme_elements |
| 1013 |
theme_source |
| 1014 |
builtin_name |
| 1015 |
custom_name |
| 1016 |
|
| 1017 |
Attributes updated: |
| 1018 |
highlight_sample: Set the tag elements to the theme. |
| 1019 |
|
| 1020 |
Methods: |
| 1021 |
set_color_sample |
| 1022 |
|
| 1023 |
Called from: |
| 1024 |
var_changed_builtin_name |
| 1025 |
var_changed_custom_name |
| 1026 |
load_theme_cfg |
| 1027 |
""" |
| 1028 |
if self.theme_source.get(): # Default theme |
| 1029 |
theme = self.builtin_name.get() |
| 1030 |
else: # User theme |
| 1031 |
theme = self.custom_name.get() |
| 1032 |
for element_title in self.theme_elements: |
| 1033 |
element = self.theme_elements[element_title] |
| 1034 |
colors = idleConf.GetHighlight(theme, element) |
| 1035 |
if element == 'cursor': # Cursor sample needs special painting. |
| 1036 |
colors['background'] = idleConf.GetHighlight( |
| 1037 |
theme, 'normal')['background'] |
| 1038 |
# Handle any unsaved changes to this theme. |
| 1039 |
if theme in changes['highlight']: |
| 1040 |
theme_dict = changes['highlight'][theme] |
| 1041 |
if element + '-foreground' in theme_dict: |
| 1042 |
colors['foreground'] = theme_dict[element + '-foreground'] |
| 1043 |
if element + '-background' in theme_dict: |
| 1044 |
colors['background'] = theme_dict[element + '-background'] |
| 1045 |
self.highlight_sample.tag_config(element, **colors) |
| 1046 |
self.set_color_sample() |
| 1047 |
|
| 1048 |
def save_new(self, theme_name, theme): |
| 1049 |
"""Save a newly created theme to idleConf. |
| 1050 |
|
| 1051 |
theme_name - string, the name of the new theme |
| 1052 |
theme - dictionary containing the new theme |
| 1053 |
""" |
| 1054 |
idleConf.userCfg['highlight'].AddSection(theme_name) |
| 1055 |
for element in theme: |
| 1056 |
value = theme[element] |
| 1057 |
idleConf.userCfg['highlight'].SetOption(theme_name, element, value) |
| 1058 |
|
| 1059 |
def askyesno(self, *args, **kwargs): |
| 1060 |
# Make testing easier. Could change implementation. |
| 1061 |
return messagebox.askyesno(*args, **kwargs) |
| 1062 |
|
| 1063 |
def delete_custom(self): |
| 1064 |
"""Handle event to delete custom theme. |
| 1065 |
|
| 1066 |
The current theme is deactivated and the default theme is |
| 1067 |
activated. The custom theme is permanently removed from |
| 1068 |
the config file. |
| 1069 |
|
| 1070 |
Attributes accessed: |
| 1071 |
custom_name |
| 1072 |
|
| 1073 |
Attributes updated: |
| 1074 |
custom_theme_on |
| 1075 |
customlist |
| 1076 |
theme_source |
| 1077 |
builtin_name |
| 1078 |
|
| 1079 |
Methods: |
| 1080 |
deactivate_current_config |
| 1081 |
save_all_changed_extensions |
| 1082 |
activate_config_changes |
| 1083 |
set_theme_type |
| 1084 |
""" |
| 1085 |
theme_name = self.custom_name.get() |
| 1086 |
delmsg = 'Are you sure you wish to delete the theme %r ?' |
| 1087 |
if not self.askyesno( |
| 1088 |
'Delete Theme', delmsg % theme_name, parent=self): |
| 1089 |
return |
| 1090 |
self.cd.deactivate_current_config() |
| 1091 |
# Remove theme from changes, config, and file. |
| 1092 |
changes.delete_section('highlight', theme_name) |
| 1093 |
# Reload user theme list. |
| 1094 |
item_list = idleConf.GetSectionList('user', 'highlight') |
| 1095 |
item_list.sort() |
| 1096 |
if not item_list: |
| 1097 |
self.custom_theme_on.state(('disabled',)) |
| 1098 |
self.customlist.SetMenu(item_list, '- no custom themes -') |
| 1099 |
else: |
| 1100 |
self.customlist.SetMenu(item_list, item_list[0]) |
| 1101 |
# Revert to default theme. |
| 1102 |
self.theme_source.set(idleConf.defaultCfg['main'].Get('Theme', 'default')) |
| 1103 |
self.builtin_name.set(idleConf.defaultCfg['main'].Get('Theme', 'name')) |
| 1104 |
# User can't back out of these changes, they must be applied now. |
| 1105 |
changes.save_all() |
| 1106 |
self.extpage.save_all_changed_extensions() |
| 1107 |
self.cd.activate_config_changes() |
| 1108 |
self.set_theme_type() |
| 1109 |
|
| 1110 |
|
| 1111 |
class KeysPage(Frame): |
| 1112 |
|
| 1113 |
def __init__(self, master, extpage): |
| 1114 |
super().__init__(master) |
| 1115 |
self.extpage = extpage |
| 1116 |
self.cd = master.winfo_toplevel() |
| 1117 |
self.create_page_keys() |
| 1118 |
self.load_key_cfg() |
| 1119 |
|
| 1120 |
def create_page_keys(self): |
| 1121 |
"""Return frame of widgets for Keys tab. |
| 1122 |
|
| 1123 |
Enable users to provisionally change both individual and sets of |
| 1124 |
keybindings (shortcut keys). Except for features implemented as |
| 1125 |
extensions, keybindings are stored in complete sets called |
| 1126 |
keysets. Built-in keysets in idlelib/config-keys.def are fixed |
| 1127 |
as far as the dialog is concerned. Any keyset can be used as the |
| 1128 |
base for a new custom keyset, stored in .idlerc/config-keys.cfg. |
| 1129 |
|
| 1130 |
Function load_key_cfg() initializes tk variables and keyset |
| 1131 |
lists and calls load_keys_list for the current keyset. |
| 1132 |
Radiobuttons builtin_keyset_on and custom_keyset_on toggle var |
| 1133 |
keyset_source, which controls if the current set of keybindings |
| 1134 |
are from a builtin or custom keyset. DynOptionMenus builtinlist |
| 1135 |
and customlist contain lists of the builtin and custom keysets, |
| 1136 |
respectively, and the current item from each list is stored in |
| 1137 |
vars builtin_name and custom_name. |
| 1138 |
|
| 1139 |
Button delete_custom_keys invokes delete_custom_keys() to delete |
| 1140 |
a custom keyset from idleConf.userCfg['keys'] and changes. Button |
| 1141 |
save_custom_keys invokes save_as_new_key_set() which calls |
| 1142 |
get_new_keys_name() and create_new_key_set() to save a custom keyset |
| 1143 |
and its keybindings to idleConf.userCfg['keys']. |
| 1144 |
|
| 1145 |
Listbox bindingslist contains all of the keybindings for the |
| 1146 |
selected keyset. The keybindings are loaded in load_keys_list() |
| 1147 |
and are pairs of (event, [keys]) where keys can be a list |
| 1148 |
of one or more key combinations to bind to the same event. |
| 1149 |
Mouse button 1 click invokes on_bindingslist_select(), which |
| 1150 |
allows button_new_keys to be clicked. |
| 1151 |
|
| 1152 |
So, an item is selected in listbindings, which activates |
| 1153 |
button_new_keys, and clicking button_new_keys calls function |
| 1154 |
get_new_keys(). Function get_new_keys() gets the key mappings from the |
| 1155 |
current keyset for the binding event item that was selected. The |
| 1156 |
function then displays another dialog, GetKeysDialog, with the |
| 1157 |
selected binding event and current keys and allows new key sequences |
| 1158 |
to be entered for that binding event. If the keys aren't |
| 1159 |
changed, nothing happens. If the keys are changed and the keyset |
| 1160 |
is a builtin, function get_new_keys_name() will be called |
| 1161 |
for input of a custom keyset name. If no name is given, then the |
| 1162 |
change to the keybinding will abort and no updates will be made. If |
| 1163 |
a custom name is entered in the prompt or if the current keyset was |
| 1164 |
already custom (and thus didn't require a prompt), then |
| 1165 |
idleConf.userCfg['keys'] is updated in function create_new_key_set() |
| 1166 |
with the change to the event binding. The item listing in bindingslist |
| 1167 |
is updated with the new keys. Var keybinding is also set which invokes |
| 1168 |
the callback function, var_changed_keybinding, to add the change to |
| 1169 |
the 'keys' or 'extensions' changes tracker based on the binding type. |
| 1170 |
|
| 1171 |
Tk Variables: |
| 1172 |
keybinding: Action/key bindings. |
| 1173 |
|
| 1174 |
Methods: |
| 1175 |
load_keys_list: Reload active set. |
| 1176 |
create_new_key_set: Combine active keyset and changes. |
| 1177 |
set_keys_type: Command for keyset_source. |
| 1178 |
save_new_key_set: Save to idleConf.userCfg['keys'] (is function). |
| 1179 |
deactivate_current_config: Remove keys bindings in editors. |
| 1180 |
|
| 1181 |
Widgets for KeysPage(frame): (*) widgets bound to self |
| 1182 |
frame_key_sets: LabelFrame |
| 1183 |
frames[0]: Frame |
| 1184 |
(*)builtin_keyset_on: Radiobutton - var keyset_source |
| 1185 |
(*)custom_keyset_on: Radiobutton - var keyset_source |
| 1186 |
(*)builtinlist: DynOptionMenu - var builtin_name, |
| 1187 |
func keybinding_selected |
| 1188 |
(*)customlist: DynOptionMenu - var custom_name, |
| 1189 |
func keybinding_selected |
| 1190 |
(*)keys_message: Label |
| 1191 |
frames[1]: Frame |
| 1192 |
(*)button_delete_custom_keys: Button - delete_custom_keys |
| 1193 |
(*)button_save_custom_keys: Button - save_as_new_key_set |
| 1194 |
frame_custom: LabelFrame |
| 1195 |
frame_target: Frame |
| 1196 |
target_title: Label |
| 1197 |
scroll_target_y: Scrollbar |
| 1198 |
scroll_target_x: Scrollbar |
| 1199 |
(*)bindingslist: ListBox - on_bindingslist_select |
| 1200 |
(*)button_new_keys: Button - get_new_keys & ..._name |
| 1201 |
""" |
| 1202 |
self.builtin_name = tracers.add( |
| 1203 |
StringVar(self), self.var_changed_builtin_name) |
| 1204 |
self.custom_name = tracers.add( |
| 1205 |
StringVar(self), self.var_changed_custom_name) |
| 1206 |
self.keyset_source = tracers.add( |
| 1207 |
BooleanVar(self), self.var_changed_keyset_source) |
| 1208 |
self.keybinding = tracers.add( |
| 1209 |
StringVar(self), self.var_changed_keybinding) |
| 1210 |
|
| 1211 |
# Create widgets: |
| 1212 |
# body and section frames. |
| 1213 |
frame_custom = LabelFrame( |
| 1214 |
self, borderwidth=2, relief=GROOVE, |
| 1215 |
text=' Custom Key Bindings ') |
| 1216 |
frame_key_sets = LabelFrame( |
| 1217 |
self, borderwidth=2, relief=GROOVE, text=' Key Set ') |
| 1218 |
# frame_custom. |
| 1219 |
frame_target = Frame(frame_custom) |
| 1220 |
target_title = Label(frame_target, text='Action - Key(s)') |
| 1221 |
scroll_target_y = Scrollbar(frame_target) |
| 1222 |
scroll_target_x = Scrollbar(frame_target, orient=HORIZONTAL) |
| 1223 |
self.bindingslist = Listbox( |
| 1224 |
frame_target, takefocus=FALSE, exportselection=FALSE) |
| 1225 |
self.bindingslist.bind('<ButtonRelease-1>', |
| 1226 |
self.on_bindingslist_select) |
| 1227 |
scroll_target_y['command'] = self.bindingslist.yview |
| 1228 |
scroll_target_x['command'] = self.bindingslist.xview |
| 1229 |
self.bindingslist['yscrollcommand'] = scroll_target_y.set |
| 1230 |
self.bindingslist['xscrollcommand'] = scroll_target_x.set |
| 1231 |
self.button_new_keys = Button( |
| 1232 |
frame_custom, text='Get New Keys for Selection', |
| 1233 |
command=self.get_new_keys, state='disabled') |
| 1234 |
# frame_key_sets. |
| 1235 |
frames = [Frame(frame_key_sets, padding=2, borderwidth=0) |
| 1236 |
for i in range(2)] |
| 1237 |
self.builtin_keyset_on = Radiobutton( |
| 1238 |
frames[0], variable=self.keyset_source, value=1, |
| 1239 |
command=self.set_keys_type, text='Use a Built-in Key Set') |
| 1240 |
self.custom_keyset_on = Radiobutton( |
| 1241 |
frames[0], variable=self.keyset_source, value=0, |
| 1242 |
command=self.set_keys_type, text='Use a Custom Key Set') |
| 1243 |
self.builtinlist = DynOptionMenu( |
| 1244 |
frames[0], self.builtin_name, None, command=None) |
| 1245 |
self.customlist = DynOptionMenu( |
| 1246 |
frames[0], self.custom_name, None, command=None) |
| 1247 |
self.button_delete_custom_keys = Button( |
| 1248 |
frames[1], text='Delete Custom Key Set', |
| 1249 |
command=self.delete_custom_keys) |
| 1250 |
self.button_save_custom_keys = Button( |
| 1251 |
frames[1], text='Save as New Custom Key Set', |
| 1252 |
command=self.save_as_new_key_set) |
| 1253 |
self.keys_message = Label(frames[0], borderwidth=2) |
| 1254 |
|
| 1255 |
# Pack widgets: |
| 1256 |
# body. |
| 1257 |
frame_custom.pack(side=BOTTOM, padx=5, pady=5, expand=TRUE, fill=BOTH) |
| 1258 |
frame_key_sets.pack(side=BOTTOM, padx=5, pady=5, fill=BOTH) |
| 1259 |
# frame_custom. |
| 1260 |
self.button_new_keys.pack(side=BOTTOM, fill=X, padx=5, pady=5) |
| 1261 |
frame_target.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH) |
| 1262 |
# frame_target. |
| 1263 |
frame_target.columnconfigure(0, weight=1) |
| 1264 |
frame_target.rowconfigure(1, weight=1) |
| 1265 |
target_title.grid(row=0, column=0, columnspan=2, sticky=W) |
| 1266 |
self.bindingslist.grid(row=1, column=0, sticky=NSEW) |
| 1267 |
scroll_target_y.grid(row=1, column=1, sticky=NS) |
| 1268 |
scroll_target_x.grid(row=2, column=0, sticky=EW) |
| 1269 |
# frame_key_sets. |
| 1270 |
self.builtin_keyset_on.grid(row=0, column=0, sticky=W+NS) |
| 1271 |
self.custom_keyset_on.grid(row=1, column=0, sticky=W+NS) |
| 1272 |
self.builtinlist.grid(row=0, column=1, sticky=NSEW) |
| 1273 |
self.customlist.grid(row=1, column=1, sticky=NSEW) |
| 1274 |
self.keys_message.grid(row=0, column=2, sticky=NSEW, padx=5, pady=5) |
| 1275 |
self.button_delete_custom_keys.pack(side=LEFT, fill=X, expand=True, padx=2) |
| 1276 |
self.button_save_custom_keys.pack(side=LEFT, fill=X, expand=True, padx=2) |
| 1277 |
frames[0].pack(side=TOP, fill=BOTH, expand=True) |
| 1278 |
frames[1].pack(side=TOP, fill=X, expand=True, pady=2) |
| 1279 |
|
| 1280 |
def load_key_cfg(self): |
| 1281 |
"Load current configuration settings for the keybinding options." |
| 1282 |
# Set current keys type radiobutton. |
| 1283 |
self.keyset_source.set(idleConf.GetOption( |
| 1284 |
'main', 'Keys', 'default', type='bool', default=1)) |
| 1285 |
# Set current keys. |
| 1286 |
current_option = idleConf.CurrentKeys() |
| 1287 |
# Load available keyset option menus. |
| 1288 |
if self.keyset_source.get(): # Default theme selected. |
| 1289 |
item_list = idleConf.GetSectionList('default', 'keys') |
| 1290 |
item_list.sort() |
| 1291 |
self.builtinlist.SetMenu(item_list, current_option) |
| 1292 |
item_list = idleConf.GetSectionList('user', 'keys') |
| 1293 |
item_list.sort() |
| 1294 |
if not item_list: |
| 1295 |
self.custom_keyset_on.state(('disabled',)) |
| 1296 |
self.custom_name.set('- no custom keys -') |
| 1297 |
else: |
| 1298 |
self.customlist.SetMenu(item_list, item_list[0]) |
| 1299 |
else: # User key set selected. |
| 1300 |
item_list = idleConf.GetSectionList('user', 'keys') |
| 1301 |
item_list.sort() |
| 1302 |
self.customlist.SetMenu(item_list, current_option) |
| 1303 |
item_list = idleConf.GetSectionList('default', 'keys') |
| 1304 |
item_list.sort() |
| 1305 |
self.builtinlist.SetMenu(item_list, idleConf.default_keys()) |
| 1306 |
self.set_keys_type() |
| 1307 |
# Load keyset element list. |
| 1308 |
keyset_name = idleConf.CurrentKeys() |
| 1309 |
self.load_keys_list(keyset_name) |
| 1310 |
|
| 1311 |
def var_changed_builtin_name(self, *params): |
| 1312 |
"Process selection of builtin key set." |
| 1313 |
old_keys = ( |
| 1314 |
'IDLE Classic Windows', |
| 1315 |
'IDLE Classic Unix', |
| 1316 |
'IDLE Classic Mac', |
| 1317 |
'IDLE Classic OSX', |
| 1318 |
) |
| 1319 |
value = self.builtin_name.get() |
| 1320 |
if value not in old_keys: |
| 1321 |
if idleConf.GetOption('main', 'Keys', 'name') not in old_keys: |
| 1322 |
changes.add_option('main', 'Keys', 'name', old_keys[0]) |
| 1323 |
changes.add_option('main', 'Keys', 'name2', value) |
| 1324 |
self.keys_message['text'] = 'New key set, see Help' |
| 1325 |
else: |
| 1326 |
changes.add_option('main', 'Keys', 'name', value) |
| 1327 |
changes.add_option('main', 'Keys', 'name2', '') |
| 1328 |
self.keys_message['text'] = '' |
| 1329 |
self.load_keys_list(value) |
| 1330 |
|
| 1331 |
def var_changed_custom_name(self, *params): |
| 1332 |
"Process selection of custom key set." |
| 1333 |
value = self.custom_name.get() |
| 1334 |
if value != '- no custom keys -': |
| 1335 |
changes.add_option('main', 'Keys', 'name', value) |
| 1336 |
self.load_keys_list(value) |
| 1337 |
|
| 1338 |
def var_changed_keyset_source(self, *params): |
| 1339 |
"Process toggle between builtin key set and custom key set." |
| 1340 |
value = self.keyset_source.get() |
| 1341 |
changes.add_option('main', 'Keys', 'default', value) |
| 1342 |
if value: |
| 1343 |
self.var_changed_builtin_name() |
| 1344 |
else: |
| 1345 |
self.var_changed_custom_name() |
| 1346 |
|
| 1347 |
def var_changed_keybinding(self, *params): |
| 1348 |
"Store change to a keybinding." |
| 1349 |
value = self.keybinding.get() |
| 1350 |
key_set = self.custom_name.get() |
| 1351 |
event = self.bindingslist.get(ANCHOR).split()[0] |
| 1352 |
if idleConf.IsCoreBinding(event): |
| 1353 |
changes.add_option('keys', key_set, event, value) |
| 1354 |
else: # Event is an extension binding. |
| 1355 |
ext_name = idleConf.GetExtnNameForEvent(event) |
| 1356 |
ext_keybind_section = ext_name + '_cfgBindings' |
| 1357 |
changes.add_option('extensions', ext_keybind_section, event, value) |
| 1358 |
|
| 1359 |
def set_keys_type(self): |
| 1360 |
"Set available screen options based on builtin or custom key set." |
| 1361 |
if self.keyset_source.get(): |
| 1362 |
self.builtinlist['state'] = 'normal' |
| 1363 |
self.customlist['state'] = 'disabled' |
| 1364 |
self.button_delete_custom_keys.state(('disabled',)) |
| 1365 |
else: |
| 1366 |
self.builtinlist['state'] = 'disabled' |
| 1367 |
self.custom_keyset_on.state(('!disabled',)) |
| 1368 |
self.customlist['state'] = 'normal' |
| 1369 |
self.button_delete_custom_keys.state(('!disabled',)) |
| 1370 |
|
| 1371 |
def get_new_keys(self): |
| 1372 |
"""Handle event to change key binding for selected line. |
| 1373 |
|
| 1374 |
A selection of a key/binding in the list of current |
| 1375 |
bindings pops up a dialog to enter a new binding. If |
| 1376 |
the current key set is builtin and a binding has |
| 1377 |
changed, then a name for a custom key set needs to be |
| 1378 |
entered for the change to be applied. |
| 1379 |
""" |
| 1380 |
list_index = self.bindingslist.index(ANCHOR) |
| 1381 |
binding = self.bindingslist.get(list_index) |
| 1382 |
bind_name = binding.split()[0] |
| 1383 |
if self.keyset_source.get(): |
| 1384 |
current_key_set_name = self.builtin_name.get() |
| 1385 |
else: |
| 1386 |
current_key_set_name = self.custom_name.get() |
| 1387 |
current_bindings = idleConf.GetCurrentKeySet() |
| 1388 |
if current_key_set_name in changes['keys']: # unsaved changes |
| 1389 |
key_set_changes = changes['keys'][current_key_set_name] |
| 1390 |
for event in key_set_changes: |
| 1391 |
current_bindings[event] = key_set_changes[event].split() |
| 1392 |
current_key_sequences = list(current_bindings.values()) |
| 1393 |
new_keys = GetKeysWindow(self, 'Get New Keys', bind_name, |
| 1394 |
current_key_sequences).result |
| 1395 |
if new_keys: |
| 1396 |
if self.keyset_source.get(): # Current key set is a built-in. |
| 1397 |
message = ('Your changes will be saved as a new Custom Key Set.' |
| 1398 |
' Enter a name for your new Custom Key Set below.') |
| 1399 |
new_keyset = self.get_new_keys_name(message) |
| 1400 |
if not new_keyset: # User cancelled custom key set creation. |
| 1401 |
self.bindingslist.select_set(list_index) |
| 1402 |
self.bindingslist.select_anchor(list_index) |
| 1403 |
return |
| 1404 |
else: # Create new custom key set based on previously active key set. |
| 1405 |
self.create_new_key_set(new_keyset) |
| 1406 |
self.bindingslist.delete(list_index) |
| 1407 |
self.bindingslist.insert(list_index, bind_name+' - '+new_keys) |
| 1408 |
self.bindingslist.select_set(list_index) |
| 1409 |
self.bindingslist.select_anchor(list_index) |
| 1410 |
self.keybinding.set(new_keys) |
| 1411 |
else: |
| 1412 |
self.bindingslist.select_set(list_index) |
| 1413 |
self.bindingslist.select_anchor(list_index) |
| 1414 |
|
| 1415 |
def get_new_keys_name(self, message): |
| 1416 |
"Return new key set name from query popup." |
| 1417 |
used_names = (idleConf.GetSectionList('user', 'keys') + |
| 1418 |
idleConf.GetSectionList('default', 'keys')) |
| 1419 |
new_keyset = SectionName( |
| 1420 |
self, 'New Custom Key Set', message, used_names).result |
| 1421 |
return new_keyset |
| 1422 |
|
| 1423 |
def save_as_new_key_set(self): |
| 1424 |
"Prompt for name of new key set and save changes using that name." |
| 1425 |
new_keys_name = self.get_new_keys_name('New Key Set Name:') |
| 1426 |
if new_keys_name: |
| 1427 |
self.create_new_key_set(new_keys_name) |
| 1428 |
|
| 1429 |
def on_bindingslist_select(self, event): |
| 1430 |
"Activate button to assign new keys to selected action." |
| 1431 |
self.button_new_keys.state(('!disabled',)) |
| 1432 |
|
| 1433 |
def create_new_key_set(self, new_key_set_name): |
| 1434 |
"""Create a new custom key set with the given name. |
| 1435 |
|
| 1436 |
Copy the bindings/keys from the previously active keyset |
| 1437 |
to the new keyset and activate the new custom keyset. |
| 1438 |
""" |
| 1439 |
if self.keyset_source.get(): |
| 1440 |
prev_key_set_name = self.builtin_name.get() |
| 1441 |
else: |
| 1442 |
prev_key_set_name = self.custom_name.get() |
| 1443 |
prev_keys = idleConf.GetCoreKeys(prev_key_set_name) |
| 1444 |
new_keys = {} |
| 1445 |
for event in prev_keys: # Add key set to changed items. |
| 1446 |
event_name = event[2:-2] # Trim off the angle brackets. |
| 1447 |
binding = ' '.join(prev_keys[event]) |
| 1448 |
new_keys[event_name] = binding |
| 1449 |
# Handle any unsaved changes to prev key set. |
| 1450 |
if prev_key_set_name in changes['keys']: |
| 1451 |
key_set_changes = changes['keys'][prev_key_set_name] |
| 1452 |
for event in key_set_changes: |
| 1453 |
new_keys[event] = key_set_changes[event] |
| 1454 |
# Save the new key set. |
| 1455 |
self.save_new_key_set(new_key_set_name, new_keys) |
| 1456 |
# Change GUI over to the new key set. |
| 1457 |
custom_key_list = idleConf.GetSectionList('user', 'keys') |
| 1458 |
custom_key_list.sort() |
| 1459 |
self.customlist.SetMenu(custom_key_list, new_key_set_name) |
| 1460 |
self.keyset_source.set(0) |
| 1461 |
self.set_keys_type() |
| 1462 |
|
| 1463 |
def load_keys_list(self, keyset_name): |
| 1464 |
"""Reload the list of action/key binding pairs for the active key set. |
| 1465 |
|
| 1466 |
An action/key binding can be selected to change the key binding. |
| 1467 |
""" |
| 1468 |
reselect = False |
| 1469 |
if self.bindingslist.curselection(): |
| 1470 |
reselect = True |
| 1471 |
list_index = self.bindingslist.index(ANCHOR) |
| 1472 |
keyset = idleConf.GetKeySet(keyset_name) |
| 1473 |
# 'set' is dict mapping virtual event to list of key events. |
| 1474 |
bind_names = list(keyset) |
| 1475 |
bind_names.sort() |
| 1476 |
self.bindingslist.delete(0, END) |
| 1477 |
for bind_name in bind_names: |
| 1478 |
key = ' '.join(keyset[bind_name]) |
| 1479 |
bind_name = bind_name[2:-2] # Trim double angle brackets. |
| 1480 |
if keyset_name in changes['keys']: |
| 1481 |
# Handle any unsaved changes to this key set. |
| 1482 |
if bind_name in changes['keys'][keyset_name]: |
| 1483 |
key = changes['keys'][keyset_name][bind_name] |
| 1484 |
self.bindingslist.insert(END, bind_name+' - '+key) |
| 1485 |
if reselect: |
| 1486 |
self.bindingslist.see(list_index) |
| 1487 |
self.bindingslist.select_set(list_index) |
| 1488 |
self.bindingslist.select_anchor(list_index) |
| 1489 |
|
| 1490 |
@staticmethod |
| 1491 |
def save_new_key_set(keyset_name, keyset): |
| 1492 |
"""Save a newly created core key set. |
| 1493 |
|
| 1494 |
Add keyset to idleConf.userCfg['keys'], not to disk. |
| 1495 |
If the keyset doesn't exist, it is created. The |
| 1496 |
binding/keys are taken from the keyset argument. |
| 1497 |
|
| 1498 |
keyset_name - string, the name of the new key set |
| 1499 |
keyset - dictionary containing the new keybindings |
| 1500 |
""" |
| 1501 |
idleConf.userCfg['keys'].AddSection(keyset_name) |
| 1502 |
for event in keyset: |
| 1503 |
value = keyset[event] |
| 1504 |
idleConf.userCfg['keys'].SetOption(keyset_name, event, value) |
| 1505 |
|
| 1506 |
def askyesno(self, *args, **kwargs): |
| 1507 |
# Make testing easier. Could change implementation. |
| 1508 |
return messagebox.askyesno(*args, **kwargs) |
| 1509 |
|
| 1510 |
def delete_custom_keys(self): |
| 1511 |
"""Handle event to delete a custom key set. |
| 1512 |
|
| 1513 |
Applying the delete deactivates the current configuration and |
| 1514 |
reverts to the default. The custom key set is permanently |
| 1515 |
deleted from the config file. |
| 1516 |
""" |
| 1517 |
keyset_name = self.custom_name.get() |
| 1518 |
delmsg = 'Are you sure you wish to delete the key set %r ?' |
| 1519 |
if not self.askyesno( |
| 1520 |
'Delete Key Set', delmsg % keyset_name, parent=self): |
| 1521 |
return |
| 1522 |
self.cd.deactivate_current_config() |
| 1523 |
# Remove key set from changes, config, and file. |
| 1524 |
changes.delete_section('keys', keyset_name) |
| 1525 |
# Reload user key set list. |
| 1526 |
item_list = idleConf.GetSectionList('user', 'keys') |
| 1527 |
item_list.sort() |
| 1528 |
if not item_list: |
| 1529 |
self.custom_keyset_on.state(('disabled',)) |
| 1530 |
self.customlist.SetMenu(item_list, '- no custom keys -') |
| 1531 |
else: |
| 1532 |
self.customlist.SetMenu(item_list, item_list[0]) |
| 1533 |
# Revert to default key set. |
| 1534 |
self.keyset_source.set(idleConf.defaultCfg['main'] |
| 1535 |
.Get('Keys', 'default')) |
| 1536 |
self.builtin_name.set(idleConf.defaultCfg['main'].Get('Keys', 'name') |
| 1537 |
or idleConf.default_keys()) |
| 1538 |
# User can't back out of these changes, they must be applied now. |
| 1539 |
changes.save_all() |
| 1540 |
self.extpage.save_all_changed_extensions() |
| 1541 |
self.cd.activate_config_changes() |
| 1542 |
self.set_keys_type() |
| 1543 |
|
| 1544 |
|
| 1545 |
class WinPage(Frame): |
| 1546 |
|
| 1547 |
def __init__(self, master): |
| 1548 |
super().__init__(master) |
| 1549 |
|
| 1550 |
self.init_validators() |
| 1551 |
self.create_page_windows() |
| 1552 |
self.load_windows_cfg() |
| 1553 |
|
| 1554 |
def init_validators(self): |
| 1555 |
digits_or_empty_re = re.compile(r'[0-9]*') |
| 1556 |
def is_digits_or_empty(s): |
| 1557 |
"Return 's is blank or contains only digits'" |
| 1558 |
return digits_or_empty_re.fullmatch(s) is not None |
| 1559 |
self.digits_only = (self.register(is_digits_or_empty), '%P',) |
| 1560 |
|
| 1561 |
def create_page_windows(self): |
| 1562 |
"""Return frame of widgets for Windows tab. |
| 1563 |
|
| 1564 |
Enable users to provisionally change general window options. |
| 1565 |
Function load_windows_cfg initializes tk variable idleConf. |
| 1566 |
Radiobuttons startup_shell_on and startup_editor_on set var |
| 1567 |
startup_edit. Entry boxes win_width_int and win_height_int set var |
| 1568 |
win_width and win_height. Setting var_name invokes the default |
| 1569 |
callback that adds option to changes. |
| 1570 |
|
| 1571 |
Widgets for WinPage(Frame): > vars, bound to self |
| 1572 |
frame_window: LabelFrame |
| 1573 |
frame_run: Frame |
| 1574 |
startup_title: Label |
| 1575 |
startup_editor_on: Radiobutton > startup_edit |
| 1576 |
startup_shell_on: Radiobutton > startup_edit |
| 1577 |
frame_win_size: Frame |
| 1578 |
win_size_title: Label |
| 1579 |
win_width_title: Label |
| 1580 |
win_width_int: Entry > win_width |
| 1581 |
win_height_title: Label |
| 1582 |
win_height_int: Entry > win_height |
| 1583 |
frame_cursor: Frame |
| 1584 |
indent_title: Label |
| 1585 |
indent_chooser: Spinbox > indent_spaces |
| 1586 |
blink_on: Checkbutton > cursor_blink |
| 1587 |
frame_autocomplete: Frame |
| 1588 |
auto_wait_title: Label |
| 1589 |
auto_wait_int: Entry > autocomplete_wait |
| 1590 |
frame_paren1: Frame |
| 1591 |
paren_style_title: Label |
| 1592 |
paren_style_type: OptionMenu > paren_style |
| 1593 |
frame_paren2: Frame |
| 1594 |
paren_time_title: Label |
| 1595 |
paren_flash_time: Entry > flash_delay |
| 1596 |
bell_on: Checkbutton > paren_bell |
| 1597 |
frame_format: Frame |
| 1598 |
format_width_title: Label |
| 1599 |
format_width_int: Entry > format_width |
| 1600 |
""" |
| 1601 |
# Integer values need StringVar because int('') raises. |
| 1602 |
self.startup_edit = tracers.add( |
| 1603 |
IntVar(self), ('main', 'General', 'editor-on-startup')) |
| 1604 |
self.win_width = tracers.add( |
| 1605 |
StringVar(self), ('main', 'EditorWindow', 'width')) |
| 1606 |
self.win_height = tracers.add( |
| 1607 |
StringVar(self), ('main', 'EditorWindow', 'height')) |
| 1608 |
self.indent_spaces = tracers.add( |
| 1609 |
StringVar(self), ('main', 'Indent', 'num-spaces')) |
| 1610 |
self.cursor_blink = tracers.add( |
| 1611 |
BooleanVar(self), ('main', 'EditorWindow', 'cursor-blink')) |
| 1612 |
self.autocomplete_wait = tracers.add( |
| 1613 |
StringVar(self), ('extensions', 'AutoComplete', 'popupwait')) |
| 1614 |
self.paren_style = tracers.add( |
| 1615 |
StringVar(self), ('extensions', 'ParenMatch', 'style')) |
| 1616 |
self.flash_delay = tracers.add( |
| 1617 |
StringVar(self), ('extensions', 'ParenMatch', 'flash-delay')) |
| 1618 |
self.paren_bell = tracers.add( |
| 1619 |
BooleanVar(self), ('extensions', 'ParenMatch', 'bell')) |
| 1620 |
self.format_width = tracers.add( |
| 1621 |
StringVar(self), ('extensions', 'FormatParagraph', 'max-width')) |
| 1622 |
|
| 1623 |
# Create widgets: |
| 1624 |
frame_window = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 1625 |
text=' Window Preferences') |
| 1626 |
|
| 1627 |
frame_run = Frame(frame_window, borderwidth=0) |
| 1628 |
startup_title = Label(frame_run, text='At Startup') |
| 1629 |
self.startup_editor_on = Radiobutton( |
| 1630 |
frame_run, variable=self.startup_edit, value=1, |
| 1631 |
text="Open Edit Window") |
| 1632 |
self.startup_shell_on = Radiobutton( |
| 1633 |
frame_run, variable=self.startup_edit, value=0, |
| 1634 |
text='Open Shell Window') |
| 1635 |
|
| 1636 |
frame_win_size = Frame(frame_window, borderwidth=0) |
| 1637 |
win_size_title = Label( |
| 1638 |
frame_win_size, text='Initial Window Size (in characters)') |
| 1639 |
win_width_title = Label(frame_win_size, text='Width') |
| 1640 |
self.win_width_int = Entry( |
| 1641 |
frame_win_size, textvariable=self.win_width, width=3, |
| 1642 |
validatecommand=self.digits_only, validate='key', |
| 1643 |
) |
| 1644 |
win_height_title = Label(frame_win_size, text='Height') |
| 1645 |
self.win_height_int = Entry( |
| 1646 |
frame_win_size, textvariable=self.win_height, width=3, |
| 1647 |
validatecommand=self.digits_only, validate='key', |
| 1648 |
) |
| 1649 |
|
| 1650 |
frame_cursor = Frame(frame_window, borderwidth=0) |
| 1651 |
indent_title = Label(frame_cursor, |
| 1652 |
text='Indent spaces (4 is standard)') |
| 1653 |
try: |
| 1654 |
self.indent_chooser = Spinbox( |
| 1655 |
frame_cursor, textvariable=self.indent_spaces, |
| 1656 |
from_=1, to=10, width=2, |
| 1657 |
validatecommand=self.digits_only, validate='key') |
| 1658 |
except TclError: |
| 1659 |
self.indent_chooser = Combobox( |
| 1660 |
frame_cursor, textvariable=self.indent_spaces, |
| 1661 |
state="readonly", values=list(range(1,11)), width=3) |
| 1662 |
cursor_blink_title = Label(frame_cursor, text='Cursor Blink') |
| 1663 |
self.cursor_blink_bool = Checkbutton(frame_cursor, text="Cursor blink", |
| 1664 |
variable=self.cursor_blink) |
| 1665 |
|
| 1666 |
frame_autocomplete = Frame(frame_window, borderwidth=0,) |
| 1667 |
auto_wait_title = Label(frame_autocomplete, |
| 1668 |
text='Completions Popup Wait (milliseconds)') |
| 1669 |
self.auto_wait_int = Entry( |
| 1670 |
frame_autocomplete, textvariable=self.autocomplete_wait, |
| 1671 |
width=6, validatecommand=self.digits_only, validate='key') |
| 1672 |
|
| 1673 |
frame_paren1 = Frame(frame_window, borderwidth=0) |
| 1674 |
paren_style_title = Label(frame_paren1, text='Paren Match Style') |
| 1675 |
self.paren_style_type = OptionMenu( |
| 1676 |
frame_paren1, self.paren_style, 'expression', |
| 1677 |
"opener","parens","expression") |
| 1678 |
frame_paren2 = Frame(frame_window, borderwidth=0) |
| 1679 |
paren_time_title = Label( |
| 1680 |
frame_paren2, text='Time Match Displayed (milliseconds)\n' |
| 1681 |
'(0 is until next input)') |
| 1682 |
self.paren_flash_time = Entry( |
| 1683 |
frame_paren2, textvariable=self.flash_delay, width=6, |
| 1684 |
validatecommand=self.digits_only, validate='key') |
| 1685 |
self.bell_on = Checkbutton( |
| 1686 |
frame_paren2, text="Bell on Mismatch", variable=self.paren_bell) |
| 1687 |
frame_format = Frame(frame_window, borderwidth=0) |
| 1688 |
format_width_title = Label(frame_format, |
| 1689 |
text='Format Paragraph Max Width') |
| 1690 |
self.format_width_int = Entry( |
| 1691 |
frame_format, textvariable=self.format_width, width=4, |
| 1692 |
validatecommand=self.digits_only, validate='key', |
| 1693 |
) |
| 1694 |
|
| 1695 |
# Pack widgets: |
| 1696 |
frame_window.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) |
| 1697 |
# frame_run. |
| 1698 |
frame_run.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1699 |
startup_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1700 |
self.startup_shell_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) |
| 1701 |
self.startup_editor_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) |
| 1702 |
# frame_win_size. |
| 1703 |
frame_win_size.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1704 |
win_size_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1705 |
self.win_height_int.pack(side=RIGHT, anchor=E, padx=10, pady=5) |
| 1706 |
win_height_title.pack(side=RIGHT, anchor=E, pady=5) |
| 1707 |
self.win_width_int.pack(side=RIGHT, anchor=E, padx=10, pady=5) |
| 1708 |
win_width_title.pack(side=RIGHT, anchor=E, pady=5) |
| 1709 |
# frame_cursor. |
| 1710 |
frame_cursor.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1711 |
indent_title.pack(side=LEFT, anchor=W, padx=5) |
| 1712 |
self.indent_chooser.pack(side=LEFT, anchor=W, padx=10) |
| 1713 |
self.cursor_blink_bool.pack(side=RIGHT, anchor=E, padx=15, pady=5) |
| 1714 |
# frame_autocomplete. |
| 1715 |
frame_autocomplete.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1716 |
auto_wait_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1717 |
self.auto_wait_int.pack(side=TOP, padx=10, pady=5) |
| 1718 |
# frame_paren. |
| 1719 |
frame_paren1.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1720 |
paren_style_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1721 |
self.paren_style_type.pack(side=TOP, padx=10, pady=5) |
| 1722 |
frame_paren2.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1723 |
paren_time_title.pack(side=LEFT, anchor=W, padx=5) |
| 1724 |
self.bell_on.pack(side=RIGHT, anchor=E, padx=15, pady=5) |
| 1725 |
self.paren_flash_time.pack(side=TOP, anchor=W, padx=15, pady=5) |
| 1726 |
# frame_format. |
| 1727 |
frame_format.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1728 |
format_width_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1729 |
self.format_width_int.pack(side=TOP, padx=10, pady=5) |
| 1730 |
|
| 1731 |
def load_windows_cfg(self): |
| 1732 |
# Set variables for all windows. |
| 1733 |
self.startup_edit.set(idleConf.GetOption( |
| 1734 |
'main', 'General', 'editor-on-startup', type='bool')) |
| 1735 |
self.win_width.set(idleConf.GetOption( |
| 1736 |
'main', 'EditorWindow', 'width', type='int')) |
| 1737 |
self.win_height.set(idleConf.GetOption( |
| 1738 |
'main', 'EditorWindow', 'height', type='int')) |
| 1739 |
self.indent_spaces.set(idleConf.GetOption( |
| 1740 |
'main', 'Indent', 'num-spaces', type='int')) |
| 1741 |
self.cursor_blink.set(idleConf.GetOption( |
| 1742 |
'main', 'EditorWindow', 'cursor-blink', type='bool')) |
| 1743 |
self.autocomplete_wait.set(idleConf.GetOption( |
| 1744 |
'extensions', 'AutoComplete', 'popupwait', type='int')) |
| 1745 |
self.paren_style.set(idleConf.GetOption( |
| 1746 |
'extensions', 'ParenMatch', 'style')) |
| 1747 |
self.flash_delay.set(idleConf.GetOption( |
| 1748 |
'extensions', 'ParenMatch', 'flash-delay', type='int')) |
| 1749 |
self.paren_bell.set(idleConf.GetOption( |
| 1750 |
'extensions', 'ParenMatch', 'bell')) |
| 1751 |
self.format_width.set(idleConf.GetOption( |
| 1752 |
'extensions', 'FormatParagraph', 'max-width', type='int')) |
| 1753 |
|
| 1754 |
|
| 1755 |
class ShedPage(Frame): |
| 1756 |
|
| 1757 |
def __init__(self, master): |
| 1758 |
super().__init__(master) |
| 1759 |
|
| 1760 |
self.init_validators() |
| 1761 |
self.create_page_shed() |
| 1762 |
self.load_shelled_cfg() |
| 1763 |
|
| 1764 |
def init_validators(self): |
| 1765 |
digits_or_empty_re = re.compile(r'[0-9]*') |
| 1766 |
def is_digits_or_empty(s): |
| 1767 |
"Return 's is blank or contains only digits'" |
| 1768 |
return digits_or_empty_re.fullmatch(s) is not None |
| 1769 |
self.digits_only = (self.register(is_digits_or_empty), '%P',) |
| 1770 |
|
| 1771 |
def create_page_shed(self): |
| 1772 |
"""Return frame of widgets for Shell/Ed tab. |
| 1773 |
|
| 1774 |
Enable users to provisionally change shell and editor options. |
| 1775 |
Function load_shed_cfg initializes tk variables using idleConf. |
| 1776 |
Entry box auto_squeeze_min_lines_int sets |
| 1777 |
auto_squeeze_min_lines_int. Setting var_name invokes the |
| 1778 |
default callback that adds option to changes. |
| 1779 |
|
| 1780 |
Widgets for ShedPage(Frame): (*) widgets bound to self |
| 1781 |
frame_shell: LabelFrame |
| 1782 |
frame_auto_squeeze_min_lines: Frame |
| 1783 |
auto_squeeze_min_lines_title: Label |
| 1784 |
(*)auto_squeeze_min_lines_int: Entry - |
| 1785 |
auto_squeeze_min_lines |
| 1786 |
frame_editor: LabelFrame |
| 1787 |
frame_save: Frame |
| 1788 |
run_save_title: Label |
| 1789 |
(*)save_ask_on: Radiobutton - autosave |
| 1790 |
(*)save_auto_on: Radiobutton - autosave |
| 1791 |
frame_format: Frame |
| 1792 |
format_width_title: Label |
| 1793 |
(*)format_width_int: Entry - format_width |
| 1794 |
frame_line_numbers_default: Frame |
| 1795 |
line_numbers_default_title: Label |
| 1796 |
(*)line_numbers_default_bool: Checkbutton - line_numbers_default |
| 1797 |
frame_context: Frame |
| 1798 |
context_title: Label |
| 1799 |
(*)context_int: Entry - context_lines |
| 1800 |
""" |
| 1801 |
# Integer values need StringVar because int('') raises. |
| 1802 |
self.auto_squeeze_min_lines = tracers.add( |
| 1803 |
StringVar(self), ('main', 'PyShell', 'auto-squeeze-min-lines')) |
| 1804 |
|
| 1805 |
self.autosave = tracers.add( |
| 1806 |
IntVar(self), ('main', 'General', 'autosave')) |
| 1807 |
self.line_numbers_default = tracers.add( |
| 1808 |
BooleanVar(self), |
| 1809 |
('main', 'EditorWindow', 'line-numbers-default')) |
| 1810 |
self.context_lines = tracers.add( |
| 1811 |
StringVar(self), ('extensions', 'CodeContext', 'maxlines')) |
| 1812 |
|
| 1813 |
# Create widgets: |
| 1814 |
frame_shell = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 1815 |
text=' Shell Preferences') |
| 1816 |
frame_editor = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 1817 |
text=' Editor Preferences') |
| 1818 |
# Frame_shell. |
| 1819 |
frame_auto_squeeze_min_lines = Frame(frame_shell, borderwidth=0) |
| 1820 |
auto_squeeze_min_lines_title = Label(frame_auto_squeeze_min_lines, |
| 1821 |
text='Auto-Squeeze Min. Lines:') |
| 1822 |
self.auto_squeeze_min_lines_int = Entry( |
| 1823 |
frame_auto_squeeze_min_lines, width=4, |
| 1824 |
textvariable=self.auto_squeeze_min_lines, |
| 1825 |
validatecommand=self.digits_only, validate='key', |
| 1826 |
) |
| 1827 |
# Frame_editor. |
| 1828 |
frame_save = Frame(frame_editor, borderwidth=0) |
| 1829 |
run_save_title = Label(frame_save, text='At Start of Run (F5) ') |
| 1830 |
|
| 1831 |
self.save_ask_on = Radiobutton( |
| 1832 |
frame_save, variable=self.autosave, value=0, |
| 1833 |
text="Prompt to Save") |
| 1834 |
self.save_auto_on = Radiobutton( |
| 1835 |
frame_save, variable=self.autosave, value=1, |
| 1836 |
text='No Prompt') |
| 1837 |
|
| 1838 |
frame_line_numbers_default = Frame(frame_editor, borderwidth=0) |
| 1839 |
line_numbers_default_title = Label( |
| 1840 |
frame_line_numbers_default, text='Show line numbers in new windows') |
| 1841 |
self.line_numbers_default_bool = Checkbutton( |
| 1842 |
frame_line_numbers_default, |
| 1843 |
variable=self.line_numbers_default, |
| 1844 |
width=1) |
| 1845 |
|
| 1846 |
frame_context = Frame(frame_editor, borderwidth=0) |
| 1847 |
context_title = Label(frame_context, text='Max Context Lines :') |
| 1848 |
self.context_int = Entry( |
| 1849 |
frame_context, textvariable=self.context_lines, width=3, |
| 1850 |
validatecommand=self.digits_only, validate='key', |
| 1851 |
) |
| 1852 |
|
| 1853 |
# Pack widgets: |
| 1854 |
frame_shell.pack(side=TOP, padx=5, pady=5, fill=BOTH) |
| 1855 |
Label(self).pack() # Spacer -- better solution? |
| 1856 |
frame_editor.pack(side=TOP, padx=5, pady=5, fill=BOTH) |
| 1857 |
# frame_auto_squeeze_min_lines |
| 1858 |
frame_auto_squeeze_min_lines.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1859 |
auto_squeeze_min_lines_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1860 |
self.auto_squeeze_min_lines_int.pack(side=TOP, padx=5, pady=5) |
| 1861 |
# frame_save. |
| 1862 |
frame_save.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1863 |
run_save_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1864 |
self.save_auto_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) |
| 1865 |
self.save_ask_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) |
| 1866 |
# frame_line_numbers_default. |
| 1867 |
frame_line_numbers_default.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1868 |
line_numbers_default_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1869 |
self.line_numbers_default_bool.pack(side=LEFT, padx=5, pady=5) |
| 1870 |
# frame_context. |
| 1871 |
frame_context.pack(side=TOP, padx=5, pady=0, fill=X) |
| 1872 |
context_title.pack(side=LEFT, anchor=W, padx=5, pady=5) |
| 1873 |
self.context_int.pack(side=TOP, padx=5, pady=5) |
| 1874 |
|
| 1875 |
def load_shelled_cfg(self): |
| 1876 |
# Set variables for shell windows. |
| 1877 |
self.auto_squeeze_min_lines.set(idleConf.GetOption( |
| 1878 |
'main', 'PyShell', 'auto-squeeze-min-lines', type='int')) |
| 1879 |
# Set variables for editor windows. |
| 1880 |
self.autosave.set(idleConf.GetOption( |
| 1881 |
'main', 'General', 'autosave', default=0, type='bool')) |
| 1882 |
self.line_numbers_default.set(idleConf.GetOption( |
| 1883 |
'main', 'EditorWindow', 'line-numbers-default', type='bool')) |
| 1884 |
self.context_lines.set(idleConf.GetOption( |
| 1885 |
'extensions', 'CodeContext', 'maxlines', type='int')) |
| 1886 |
|
| 1887 |
|
| 1888 |
class ExtPage(Frame): |
| 1889 |
def __init__(self, master): |
| 1890 |
super().__init__(master) |
| 1891 |
self.ext_defaultCfg = idleConf.defaultCfg['extensions'] |
| 1892 |
self.ext_userCfg = idleConf.userCfg['extensions'] |
| 1893 |
self.is_int = self.register(is_int) |
| 1894 |
self.load_extensions() |
| 1895 |
self.create_page_extensions() # Requires extension names. |
| 1896 |
|
| 1897 |
def create_page_extensions(self): |
| 1898 |
"""Configure IDLE feature extensions and help menu extensions. |
| 1899 |
|
| 1900 |
List the feature extensions and a configuration box for the |
| 1901 |
selected extension. Help menu extensions are in a HelpFrame. |
| 1902 |
|
| 1903 |
This code reads the current configuration using idleConf, |
| 1904 |
supplies a GUI interface to change the configuration values, |
| 1905 |
and saves the changes using idleConf. |
| 1906 |
|
| 1907 |
Some changes may require restarting IDLE. This depends on each |
| 1908 |
extension's implementation. |
| 1909 |
|
| 1910 |
All values are treated as text, and it is up to the user to |
| 1911 |
supply reasonable values. The only exception to this are the |
| 1912 |
'enable*' options, which are boolean, and can be toggled with a |
| 1913 |
True/False button. |
| 1914 |
|
| 1915 |
Methods: |
| 1916 |
extension_selected: Handle selection from list. |
| 1917 |
create_extension_frame: Hold widgets for one extension. |
| 1918 |
set_extension_value: Set in userCfg['extensions']. |
| 1919 |
save_all_changed_extensions: Call extension page Save(). |
| 1920 |
""" |
| 1921 |
self.extension_names = StringVar(self) |
| 1922 |
|
| 1923 |
frame_ext = LabelFrame(self, borderwidth=2, relief=GROOVE, |
| 1924 |
text=' Feature Extensions ') |
| 1925 |
self.frame_help = HelpFrame(self, borderwidth=2, relief=GROOVE, |
| 1926 |
text=' Help Menu Extensions ') |
| 1927 |
|
| 1928 |
frame_ext.rowconfigure(0, weight=1) |
| 1929 |
frame_ext.columnconfigure(2, weight=1) |
| 1930 |
self.extension_list = Listbox(frame_ext, listvariable=self.extension_names, |
| 1931 |
selectmode='browse') |
| 1932 |
self.extension_list.bind('<<ListboxSelect>>', self.extension_selected) |
| 1933 |
scroll = Scrollbar(frame_ext, command=self.extension_list.yview) |
| 1934 |
self.extension_list.yscrollcommand=scroll.set |
| 1935 |
self.details_frame = LabelFrame(frame_ext, width=250, height=250) |
| 1936 |
self.extension_list.grid(column=0, row=0, sticky='nws') |
| 1937 |
scroll.grid(column=1, row=0, sticky='ns') |
| 1938 |
self.details_frame.grid(column=2, row=0, sticky='nsew', padx=[10, 0]) |
| 1939 |
frame_ext.configure(padding=10) |
| 1940 |
self.config_frame = {} |
| 1941 |
self.current_extension = None |
| 1942 |
|
| 1943 |
self.outerframe = self # TEMPORARY |
| 1944 |
self.tabbed_page_set = self.extension_list # TEMPORARY |
| 1945 |
|
| 1946 |
# Create the frame holding controls for each extension. |
| 1947 |
ext_names = '' |
| 1948 |
for ext_name in sorted(self.extensions): |
| 1949 |
self.create_extension_frame(ext_name) |
| 1950 |
ext_names = ext_names + '{' + ext_name + '} ' |
| 1951 |
self.extension_names.set(ext_names) |
| 1952 |
self.extension_list.selection_set(0) |
| 1953 |
self.extension_selected(None) |
| 1954 |
|
| 1955 |
|
| 1956 |
frame_ext.grid(row=0, column=0, sticky='nsew') |
| 1957 |
Label(self).grid(row=1, column=0) # Spacer. Replace with config? |
| 1958 |
self.frame_help.grid(row=2, column=0, sticky='sew') |
| 1959 |
|
| 1960 |
def load_extensions(self): |
| 1961 |
"Fill self.extensions with data from the default and user configs." |
| 1962 |
self.extensions = {} |
| 1963 |
for ext_name in idleConf.GetExtensions(active_only=False): |
| 1964 |
# Former built-in extensions are already filtered out. |
| 1965 |
self.extensions[ext_name] = [] |
| 1966 |
|
| 1967 |
for ext_name in self.extensions: |
| 1968 |
opt_list = sorted(self.ext_defaultCfg.GetOptionList(ext_name)) |
| 1969 |
|
| 1970 |
# Bring 'enable' options to the beginning of the list. |
| 1971 |
enables = [opt_name for opt_name in opt_list |
| 1972 |
if opt_name.startswith('enable')] |
| 1973 |
for opt_name in enables: |
| 1974 |
opt_list.remove(opt_name) |
| 1975 |
opt_list = enables + opt_list |
| 1976 |
|
| 1977 |
for opt_name in opt_list: |
| 1978 |
def_str = self.ext_defaultCfg.Get( |
| 1979 |
ext_name, opt_name, raw=True) |
| 1980 |
try: |
| 1981 |
def_obj = {'True':True, 'False':False}[def_str] |
| 1982 |
opt_type = 'bool' |
| 1983 |
except KeyError: |
| 1984 |
try: |
| 1985 |
def_obj = int(def_str) |
| 1986 |
opt_type = 'int' |
| 1987 |
except ValueError: |
| 1988 |
def_obj = def_str |
| 1989 |
opt_type = None |
| 1990 |
try: |
| 1991 |
value = self.ext_userCfg.Get( |
| 1992 |
ext_name, opt_name, type=opt_type, raw=True, |
| 1993 |
default=def_obj) |
| 1994 |
except ValueError: # Need this until .Get fixed. |
| 1995 |
value = def_obj # Bad values overwritten by entry. |
| 1996 |
var = StringVar(self) |
| 1997 |
var.set(str(value)) |
| 1998 |
|
| 1999 |
self.extensions[ext_name].append({'name': opt_name, |
| 2000 |
'type': opt_type, |
| 2001 |
'default': def_str, |
| 2002 |
'value': value, |
| 2003 |
'var': var, |
| 2004 |
}) |
| 2005 |
|
| 2006 |
def extension_selected(self, event): |
| 2007 |
"Handle selection of an extension from the list." |
| 2008 |
newsel = self.extension_list.curselection() |
| 2009 |
if newsel: |
| 2010 |
newsel = self.extension_list.get(newsel) |
| 2011 |
if newsel is None or newsel != self.current_extension: |
| 2012 |
if self.current_extension: |
| 2013 |
self.details_frame.config(text='') |
| 2014 |
self.config_frame[self.current_extension].grid_forget() |
| 2015 |
self.current_extension = None |
| 2016 |
if newsel: |
| 2017 |
self.details_frame.config(text=newsel) |
| 2018 |
self.config_frame[newsel].grid(column=0, row=0, sticky='nsew') |
| 2019 |
self.current_extension = newsel |
| 2020 |
|
| 2021 |
def create_extension_frame(self, ext_name): |
| 2022 |
"""Create a frame holding the widgets to configure one extension""" |
| 2023 |
f = VerticalScrolledFrame(self.details_frame, height=250, width=250) |
| 2024 |
self.config_frame[ext_name] = f |
| 2025 |
entry_area = f.interior |
| 2026 |
# Create an entry for each configuration option. |
| 2027 |
for row, opt in enumerate(self.extensions[ext_name]): |
| 2028 |
# Create a row with a label and entry/checkbutton. |
| 2029 |
label = Label(entry_area, text=opt['name']) |
| 2030 |
label.grid(row=row, column=0, sticky=NW) |
| 2031 |
var = opt['var'] |
| 2032 |
if opt['type'] == 'bool': |
| 2033 |
Checkbutton(entry_area, variable=var, |
| 2034 |
onvalue='True', offvalue='False', width=8 |
| 2035 |
).grid(row=row, column=1, sticky=W, padx=7) |
| 2036 |
elif opt['type'] == 'int': |
| 2037 |
Entry(entry_area, textvariable=var, validate='key', |
| 2038 |
validatecommand=(self.is_int, '%P'), width=10 |
| 2039 |
).grid(row=row, column=1, sticky=NSEW, padx=7) |
| 2040 |
|
| 2041 |
else: # type == 'str' |
| 2042 |
# Limit size to fit non-expanding space with larger font. |
| 2043 |
Entry(entry_area, textvariable=var, width=15 |
| 2044 |
).grid(row=row, column=1, sticky=NSEW, padx=7) |
| 2045 |
return |
| 2046 |
|
| 2047 |
def set_extension_value(self, section, opt): |
| 2048 |
"""Return True if the configuration was added or changed. |
| 2049 |
|
| 2050 |
If the value is the same as the default, then remove it |
| 2051 |
from user config file. |
| 2052 |
""" |
| 2053 |
name = opt['name'] |
| 2054 |
default = opt['default'] |
| 2055 |
value = opt['var'].get().strip() or default |
| 2056 |
opt['var'].set(value) |
| 2057 |
# if self.defaultCfg.has_section(section): |
| 2058 |
# Currently, always true; if not, indent to return. |
| 2059 |
if (value == default): |
| 2060 |
return self.ext_userCfg.RemoveOption(section, name) |
| 2061 |
# Set the option. |
| 2062 |
return self.ext_userCfg.SetOption(section, name, value) |
| 2063 |
|
| 2064 |
def save_all_changed_extensions(self): |
| 2065 |
"""Save configuration changes to the user config file. |
| 2066 |
|
| 2067 |
Attributes accessed: |
| 2068 |
extensions |
| 2069 |
|
| 2070 |
Methods: |
| 2071 |
set_extension_value |
| 2072 |
""" |
| 2073 |
has_changes = False |
| 2074 |
for ext_name in self.extensions: |
| 2075 |
options = self.extensions[ext_name] |
| 2076 |
for opt in options: |
| 2077 |
if self.set_extension_value(ext_name, opt): |
| 2078 |
has_changes = True |
| 2079 |
if has_changes: |
| 2080 |
self.ext_userCfg.Save() |
| 2081 |
|
| 2082 |
|
| 2083 |
class HelpFrame(LabelFrame): |
| 2084 |
|
| 2085 |
def __init__(self, master, **cfg): |
| 2086 |
super().__init__(master, **cfg) |
| 2087 |
self.create_frame_help() |
| 2088 |
self.load_helplist() |
| 2089 |
|
| 2090 |
def create_frame_help(self): |
| 2091 |
"""Create LabelFrame for additional help menu sources. |
| 2092 |
|
| 2093 |
load_helplist loads list user_helplist with |
| 2094 |
name, position pairs and copies names to listbox helplist. |
| 2095 |
Clicking a name invokes help_source selected. Clicking |
| 2096 |
button_helplist_name invokes helplist_item_name, which also |
| 2097 |
changes user_helplist. These functions all call |
| 2098 |
set_add_delete_state. All but load call update_help_changes to |
| 2099 |
rewrite changes['main']['HelpFiles']. |
| 2100 |
|
| 2101 |
Widgets for HelpFrame(LabelFrame): (*) widgets bound to self |
| 2102 |
frame_helplist: Frame |
| 2103 |
(*)helplist: ListBox |
| 2104 |
scroll_helplist: Scrollbar |
| 2105 |
frame_buttons: Frame |
| 2106 |
(*)button_helplist_edit |
| 2107 |
(*)button_helplist_add |
| 2108 |
(*)button_helplist_remove |
| 2109 |
""" |
| 2110 |
# self = frame_help in dialog (until ExtPage class). |
| 2111 |
frame_helplist = Frame(self) |
| 2112 |
self.helplist = Listbox( |
| 2113 |
frame_helplist, height=5, takefocus=True, |
| 2114 |
exportselection=FALSE) |
| 2115 |
scroll_helplist = Scrollbar(frame_helplist) |
| 2116 |
scroll_helplist['command'] = self.helplist.yview |
| 2117 |
self.helplist['yscrollcommand'] = scroll_helplist.set |
| 2118 |
self.helplist.bind('<ButtonRelease-1>', self.help_source_selected) |
| 2119 |
|
| 2120 |
frame_buttons = Frame(self) |
| 2121 |
self.button_helplist_edit = Button( |
| 2122 |
frame_buttons, text='Edit', state='disabled', |
| 2123 |
width=8, command=self.helplist_item_edit) |
| 2124 |
self.button_helplist_add = Button( |
| 2125 |
frame_buttons, text='Add', |
| 2126 |
width=8, command=self.helplist_item_add) |
| 2127 |
self.button_helplist_remove = Button( |
| 2128 |
frame_buttons, text='Remove', state='disabled', |
| 2129 |
width=8, command=self.helplist_item_remove) |
| 2130 |
|
| 2131 |
# Pack frame_help. |
| 2132 |
frame_helplist.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH) |
| 2133 |
self.helplist.pack(side=LEFT, anchor=E, expand=TRUE, fill=BOTH) |
| 2134 |
scroll_helplist.pack(side=RIGHT, anchor=W, fill=Y) |
| 2135 |
frame_buttons.pack(side=RIGHT, padx=5, pady=5, fill=Y) |
| 2136 |
self.button_helplist_edit.pack(side=TOP, anchor=W, pady=5) |
| 2137 |
self.button_helplist_add.pack(side=TOP, anchor=W) |
| 2138 |
self.button_helplist_remove.pack(side=TOP, anchor=W, pady=5) |
| 2139 |
|
| 2140 |
def help_source_selected(self, event): |
| 2141 |
"Handle event for selecting additional help." |
| 2142 |
self.set_add_delete_state() |
| 2143 |
|
| 2144 |
def set_add_delete_state(self): |
| 2145 |
"Toggle the state for the help list buttons based on list entries." |
| 2146 |
if self.helplist.size() < 1: # No entries in list. |
| 2147 |
self.button_helplist_edit.state(('disabled',)) |
| 2148 |
self.button_helplist_remove.state(('disabled',)) |
| 2149 |
else: # Some entries. |
| 2150 |
if self.helplist.curselection(): # There currently is a selection. |
| 2151 |
self.button_helplist_edit.state(('!disabled',)) |
| 2152 |
self.button_helplist_remove.state(('!disabled',)) |
| 2153 |
else: # There currently is not a selection. |
| 2154 |
self.button_helplist_edit.state(('disabled',)) |
| 2155 |
self.button_helplist_remove.state(('disabled',)) |
| 2156 |
|
| 2157 |
def helplist_item_add(self): |
| 2158 |
"""Handle add button for the help list. |
| 2159 |
|
| 2160 |
Query for name and location of new help sources and add |
| 2161 |
them to the list. |
| 2162 |
""" |
| 2163 |
help_source = HelpSource(self, 'New Help Source').result |
| 2164 |
if help_source: |
| 2165 |
self.user_helplist.append(help_source) |
| 2166 |
self.helplist.insert(END, help_source[0]) |
| 2167 |
self.update_help_changes() |
| 2168 |
|
| 2169 |
def helplist_item_edit(self): |
| 2170 |
"""Handle edit button for the help list. |
| 2171 |
|
| 2172 |
Query with existing help source information and update |
| 2173 |
config if the values are changed. |
| 2174 |
""" |
| 2175 |
item_index = self.helplist.index(ANCHOR) |
| 2176 |
help_source = self.user_helplist[item_index] |
| 2177 |
new_help_source = HelpSource( |
| 2178 |
self, 'Edit Help Source', |
| 2179 |
menuitem=help_source[0], |
| 2180 |
filepath=help_source[1], |
| 2181 |
).result |
| 2182 |
if new_help_source and new_help_source != help_source: |
| 2183 |
self.user_helplist[item_index] = new_help_source |
| 2184 |
self.helplist.delete(item_index) |
| 2185 |
self.helplist.insert(item_index, new_help_source[0]) |
| 2186 |
self.update_help_changes() |
| 2187 |
self.set_add_delete_state() # Selected will be un-selected |
| 2188 |
|
| 2189 |
def helplist_item_remove(self): |
| 2190 |
"""Handle remove button for the help list. |
| 2191 |
|
| 2192 |
Delete the help list item from config. |
| 2193 |
""" |
| 2194 |
item_index = self.helplist.index(ANCHOR) |
| 2195 |
del(self.user_helplist[item_index]) |
| 2196 |
self.helplist.delete(item_index) |
| 2197 |
self.update_help_changes() |
| 2198 |
self.set_add_delete_state() |
| 2199 |
|
| 2200 |
def update_help_changes(self): |
| 2201 |
"Clear and rebuild the HelpFiles section in changes" |
| 2202 |
changes['main']['HelpFiles'] = {} |
| 2203 |
for num in range(1, len(self.user_helplist) + 1): |
| 2204 |
changes.add_option( |
| 2205 |
'main', 'HelpFiles', str(num), |
| 2206 |
';'.join(self.user_helplist[num-1][:2])) |
| 2207 |
|
| 2208 |
def load_helplist(self): |
| 2209 |
# Set additional help sources. |
| 2210 |
self.user_helplist = idleConf.GetAllExtraHelpSourcesList() |
| 2211 |
self.helplist.delete(0, 'end') |
| 2212 |
for help_item in self.user_helplist: |
| 2213 |
self.helplist.insert(END, help_item[0]) |
| 2214 |
self.set_add_delete_state() |
| 2215 |
|
| 2216 |
|
| 2217 |
class VarTrace: |
| 2218 |
"""Maintain Tk variables trace state.""" |
| 2219 |
|
| 2220 |
def __init__(self): |
| 2221 |
"""Store Tk variables and callbacks. |
| 2222 |
|
| 2223 |
untraced: List of tuples (var, callback) |
| 2224 |
that do not have the callback attached |
| 2225 |
to the Tk var. |
| 2226 |
traced: List of tuples (var, callback) where |
| 2227 |
that callback has been attached to the var. |
| 2228 |
""" |
| 2229 |
self.untraced = [] |
| 2230 |
self.traced = [] |
| 2231 |
|
| 2232 |
def clear(self): |
| 2233 |
"Clear lists (for tests)." |
| 2234 |
# Call after all tests in a module to avoid memory leaks. |
| 2235 |
self.untraced.clear() |
| 2236 |
self.traced.clear() |
| 2237 |
|
| 2238 |
def add(self, var, callback): |
| 2239 |
"""Add (var, callback) tuple to untraced list. |
| 2240 |
|
| 2241 |
Args: |
| 2242 |
var: Tk variable instance. |
| 2243 |
callback: Either function name to be used as a callback |
| 2244 |
or a tuple with IdleConf config-type, section, and |
| 2245 |
option names used in the default callback. |
| 2246 |
|
| 2247 |
Return: |
| 2248 |
Tk variable instance. |
| 2249 |
""" |
| 2250 |
if isinstance(callback, tuple): |
| 2251 |
callback = self.make_callback(var, callback) |
| 2252 |
self.untraced.append((var, callback)) |
| 2253 |
return var |
| 2254 |
|
| 2255 |
@staticmethod |
| 2256 |
def make_callback(var, config): |
| 2257 |
"Return default callback function to add values to changes instance." |
| 2258 |
def default_callback(*params): |
| 2259 |
"Add config values to changes instance." |
| 2260 |
changes.add_option(*config, var.get()) |
| 2261 |
return default_callback |
| 2262 |
|
| 2263 |
def attach(self): |
| 2264 |
"Attach callback to all vars that are not traced." |
| 2265 |
while self.untraced: |
| 2266 |
var, callback = self.untraced.pop() |
| 2267 |
var.trace_add('write', callback) |
| 2268 |
self.traced.append((var, callback)) |
| 2269 |
|
| 2270 |
def detach(self): |
| 2271 |
"Remove callback from traced vars." |
| 2272 |
while self.traced: |
| 2273 |
var, callback = self.traced.pop() |
| 2274 |
var.trace_remove('write', var.trace_info()[0][1]) |
| 2275 |
self.untraced.append((var, callback)) |
| 2276 |
|
| 2277 |
|
| 2278 |
tracers = VarTrace() |
| 2279 |
|
| 2280 |
help_common = '''\ |
| 2281 |
When you click either the Apply or Ok buttons, settings in this |
| 2282 |
dialog that are different from IDLE's default are saved in |
| 2283 |
a .idlerc directory in your home directory. Except as noted, |
| 2284 |
these changes apply to all versions of IDLE installed on this |
| 2285 |
machine. [Cancel] only cancels changes made since the last save. |
| 2286 |
''' |
| 2287 |
help_pages = { |
| 2288 |
'Fonts/Tabs':''' |
| 2289 |
Font sample: This shows what a selection of Basic Multilingual Plane |
| 2290 |
unicode characters look like for the current font selection. If the |
| 2291 |
selected font does not define a character, Tk attempts to find another |
| 2292 |
font that does. Substitute glyphs depend on what is available on a |
| 2293 |
particular system and will not necessarily have the same size as the |
| 2294 |
font selected. Line contains 20 characters up to Devanagari, 14 for |
| 2295 |
Tamil, and 10 for East Asia. |
| 2296 |
|
| 2297 |
Hebrew and Arabic letters should display right to left, starting with |
| 2298 |
alef, \u05d0 and \u0627. Arabic digits display left to right. The |
| 2299 |
Devanagari and Tamil lines start with digits. The East Asian lines |
| 2300 |
are Chinese digits, Chinese Hanzi, Korean Hangul, and Japanese |
| 2301 |
Hiragana and Katakana. |
| 2302 |
|
| 2303 |
You can edit the font sample. Changes remain until IDLE is closed. |
| 2304 |
''', |
| 2305 |
'Highlights': ''' |
| 2306 |
Highlighting: |
| 2307 |
The IDLE Dark color theme is new in October 2015. It can only |
| 2308 |
be used with older IDLE releases if it is saved as a custom |
| 2309 |
theme, with a different name. |
| 2310 |
''', |
| 2311 |
'Keys': ''' |
| 2312 |
Keys: |
| 2313 |
The IDLE Modern Unix key set is new in June 2016. It can only |
| 2314 |
be used with older IDLE releases if it is saved as a custom |
| 2315 |
key set, with a different name. |
| 2316 |
''', |
| 2317 |
'General': ''' |
| 2318 |
General: |
| 2319 |
|
| 2320 |
AutoComplete: Popupwait is milliseconds to wait after key char, without |
| 2321 |
cursor movement, before popping up completion box. Key char is '.' after |
| 2322 |
identifier or a '/' (or '\\' on Windows) within a string. |
| 2323 |
|
| 2324 |
FormatParagraph: Max-width is max chars in lines after re-formatting. |
| 2325 |
Use with paragraphs in both strings and comment blocks. |
| 2326 |
|
| 2327 |
ParenMatch: Style indicates what is highlighted when closer is entered: |
| 2328 |
'opener' - opener '({[' corresponding to closer; 'parens' - both chars; |
| 2329 |
'expression' (default) - also everything in between. Flash-delay is how |
| 2330 |
long to highlight if cursor is not moved (0 means forever). |
| 2331 |
|
| 2332 |
CodeContext: Maxlines is the maximum number of code context lines to |
| 2333 |
display when Code Context is turned on for an editor window. |
| 2334 |
|
| 2335 |
Shell Preferences: Auto-Squeeze Min. Lines is the minimum number of lines |
| 2336 |
of output to automatically "squeeze". |
| 2337 |
''', |
| 2338 |
'Extensions': ''' |
| 2339 |
ZzDummy: This extension is provided as an example for how to create and |
| 2340 |
use an extension. Enable indicates whether the extension is active or |
| 2341 |
not; likewise enable_editor and enable_shell indicate which windows it |
| 2342 |
will be active on. For this extension, z-text is the text that will be |
| 2343 |
inserted at or removed from the beginning of the lines of selected text, |
| 2344 |
or the current line if no selection. |
| 2345 |
''', |
| 2346 |
} |
| 2347 |
|
| 2348 |
|
| 2349 |
def is_int(s): |
| 2350 |
"Return 's is blank or represents an int'" |
| 2351 |
if not s: |
| 2352 |
return True |
| 2353 |
try: |
| 2354 |
int(s) |
| 2355 |
return True |
| 2356 |
except ValueError: |
| 2357 |
return False |
| 2358 |
|
| 2359 |
|
| 2360 |
class VerticalScrolledFrame(Frame): |
| 2361 |
"""A pure Tkinter vertically scrollable frame. |
| 2362 |
|
| 2363 |
* Use the 'interior' attribute to place widgets inside the scrollable frame |
| 2364 |
* Construct and pack/place/grid normally |
| 2365 |
* This frame only allows vertical scrolling |
| 2366 |
""" |
| 2367 |
def __init__(self, parent, *args, **kw): |
| 2368 |
Frame.__init__(self, parent, *args, **kw) |
| 2369 |
|
| 2370 |
# Create a canvas object and a vertical scrollbar for scrolling it. |
| 2371 |
vscrollbar = Scrollbar(self, orient=VERTICAL) |
| 2372 |
vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE) |
| 2373 |
canvas = Canvas(self, borderwidth=0, highlightthickness=0, |
| 2374 |
yscrollcommand=vscrollbar.set, width=240) |
| 2375 |
canvas.pack(side=LEFT, fill=BOTH, expand=TRUE) |
| 2376 |
vscrollbar.config(command=canvas.yview) |
| 2377 |
|
| 2378 |
# Reset the view. |
| 2379 |
canvas.xview_moveto(0) |
| 2380 |
canvas.yview_moveto(0) |
| 2381 |
|
| 2382 |
# Create a frame inside the canvas which will be scrolled with it. |
| 2383 |
self.interior = interior = Frame(canvas) |
| 2384 |
interior_id = canvas.create_window(0, 0, window=interior, anchor=NW) |
| 2385 |
|
| 2386 |
# Track changes to the canvas and frame width and sync them, |
| 2387 |
# also updating the scrollbar. |
| 2388 |
def _configure_interior(event): |
| 2389 |
# Update the scrollbars to match the size of the inner frame. |
| 2390 |
size = (interior.winfo_reqwidth(), interior.winfo_reqheight()) |
| 2391 |
canvas.config(scrollregion="0 0 %s %s" % size) |
| 2392 |
interior.bind('<Configure>', _configure_interior) |
| 2393 |
|
| 2394 |
def _configure_canvas(event): |
| 2395 |
if interior.winfo_reqwidth() != canvas.winfo_width(): |
| 2396 |
# Update the inner frame's width to fill the canvas. |
| 2397 |
canvas.itemconfigure(interior_id, width=canvas.winfo_width()) |
| 2398 |
canvas.bind('<Configure>', _configure_canvas) |
| 2399 |
|
| 2400 |
return |
| 2401 |
|
| 2402 |
|
| 2403 |
if __name__ == '__main__': |
| 2404 |
from unittest import main |
| 2405 |
main('idlelib.idle_test.test_configdialog', verbosity=2, exit=False) |
| 2406 |
|
| 2407 |
from idlelib.idle_test.htest import run |
| 2408 |
run(ConfigDialog) |