jpayne@69: """IDLE Configuration Dialog: support user customization of IDLE by GUI jpayne@69: jpayne@69: Customize font faces, sizes, and colorization attributes. Set indentation jpayne@69: defaults. Customize keybindings. Colorization and keybindings can be jpayne@69: saved as user defined sets. Select startup options including shell/editor jpayne@69: and default window size. Define additional help sources. jpayne@69: jpayne@69: Note that tab width in IDLE is currently fixed at eight due to Tk issues. jpayne@69: Refer to comments in EditorWindow autoindent code for details. jpayne@69: jpayne@69: """ jpayne@69: import re jpayne@69: jpayne@69: from tkinter import (Toplevel, Listbox, Text, Scale, Canvas, jpayne@69: StringVar, BooleanVar, IntVar, TRUE, FALSE, jpayne@69: TOP, BOTTOM, RIGHT, LEFT, SOLID, GROOVE, jpayne@69: NONE, BOTH, X, Y, W, E, EW, NS, NSEW, NW, jpayne@69: HORIZONTAL, VERTICAL, ANCHOR, ACTIVE, END) jpayne@69: from tkinter.ttk import (Frame, LabelFrame, Button, Checkbutton, Entry, Label, jpayne@69: OptionMenu, Notebook, Radiobutton, Scrollbar, Style) jpayne@69: import tkinter.colorchooser as tkColorChooser jpayne@69: import tkinter.font as tkFont jpayne@69: from tkinter import messagebox jpayne@69: jpayne@69: from idlelib.config import idleConf, ConfigChanges jpayne@69: from idlelib.config_key import GetKeysDialog jpayne@69: from idlelib.dynoption import DynOptionMenu jpayne@69: from idlelib import macosx jpayne@69: from idlelib.query import SectionName, HelpSource jpayne@69: from idlelib.textview import view_text jpayne@69: from idlelib.autocomplete import AutoComplete jpayne@69: from idlelib.codecontext import CodeContext jpayne@69: from idlelib.parenmatch import ParenMatch jpayne@69: from idlelib.format import FormatParagraph jpayne@69: from idlelib.squeezer import Squeezer jpayne@69: from idlelib.textview import ScrollableTextFrame jpayne@69: jpayne@69: changes = ConfigChanges() jpayne@69: # Reload changed options in the following classes. jpayne@69: reloadables = (AutoComplete, CodeContext, ParenMatch, FormatParagraph, jpayne@69: Squeezer) jpayne@69: jpayne@69: jpayne@69: class ConfigDialog(Toplevel): jpayne@69: """Config dialog for IDLE. jpayne@69: """ jpayne@69: jpayne@69: def __init__(self, parent, title='', *, _htest=False, _utest=False): jpayne@69: """Show the tabbed dialog for user configuration. jpayne@69: jpayne@69: Args: jpayne@69: parent - parent of this dialog jpayne@69: title - string which is the title of this popup dialog jpayne@69: _htest - bool, change box location when running htest jpayne@69: _utest - bool, don't wait_window when running unittest jpayne@69: jpayne@69: Note: Focus set on font page fontlist. jpayne@69: jpayne@69: Methods: jpayne@69: create_widgets jpayne@69: cancel: Bound to DELETE_WINDOW protocol. jpayne@69: """ jpayne@69: Toplevel.__init__(self, parent) jpayne@69: self.parent = parent jpayne@69: if _htest: jpayne@69: parent.instance_dict = {} jpayne@69: if not _utest: jpayne@69: self.withdraw() jpayne@69: jpayne@69: self.configure(borderwidth=5) jpayne@69: self.title(title or 'IDLE Preferences') jpayne@69: x = parent.winfo_rootx() + 20 jpayne@69: y = parent.winfo_rooty() + (30 if not _htest else 150) jpayne@69: self.geometry(f'+{x}+{y}') jpayne@69: # Each theme element key is its display name. jpayne@69: # The first value of the tuple is the sample area tag name. jpayne@69: # The second value is the display name list sort index. jpayne@69: self.create_widgets() jpayne@69: self.resizable(height=FALSE, width=FALSE) jpayne@69: self.transient(parent) jpayne@69: self.protocol("WM_DELETE_WINDOW", self.cancel) jpayne@69: self.fontpage.fontlist.focus_set() jpayne@69: # XXX Decide whether to keep or delete these key bindings. jpayne@69: # Key bindings for this dialog. jpayne@69: # self.bind('', self.Cancel) #dismiss dialog, no save jpayne@69: # self.bind('', self.Apply) #apply changes, save jpayne@69: # self.bind('', self.Help) #context help jpayne@69: # Attach callbacks after loading config to avoid calling them. jpayne@69: tracers.attach() jpayne@69: jpayne@69: if not _utest: jpayne@69: self.grab_set() jpayne@69: self.wm_deiconify() jpayne@69: self.wait_window() jpayne@69: jpayne@69: def create_widgets(self): jpayne@69: """Create and place widgets for tabbed dialog. jpayne@69: jpayne@69: Widgets Bound to self: jpayne@69: note: Notebook jpayne@69: highpage: HighPage jpayne@69: fontpage: FontPage jpayne@69: keyspage: KeysPage jpayne@69: genpage: GenPage jpayne@69: extpage: self.create_page_extensions jpayne@69: jpayne@69: Methods: jpayne@69: create_action_buttons jpayne@69: load_configs: Load pages except for extensions. jpayne@69: activate_config_changes: Tell editors to reload. jpayne@69: """ jpayne@69: self.note = note = Notebook(self) jpayne@69: self.highpage = HighPage(note) jpayne@69: self.fontpage = FontPage(note, self.highpage) jpayne@69: self.keyspage = KeysPage(note) jpayne@69: self.genpage = GenPage(note) jpayne@69: self.extpage = self.create_page_extensions() jpayne@69: note.add(self.fontpage, text='Fonts/Tabs') jpayne@69: note.add(self.highpage, text='Highlights') jpayne@69: note.add(self.keyspage, text=' Keys ') jpayne@69: note.add(self.genpage, text=' General ') jpayne@69: note.add(self.extpage, text='Extensions') jpayne@69: note.enable_traversal() jpayne@69: note.pack(side=TOP, expand=TRUE, fill=BOTH) jpayne@69: self.create_action_buttons().pack(side=BOTTOM) jpayne@69: jpayne@69: def create_action_buttons(self): jpayne@69: """Return frame of action buttons for dialog. jpayne@69: jpayne@69: Methods: jpayne@69: ok jpayne@69: apply jpayne@69: cancel jpayne@69: help jpayne@69: jpayne@69: Widget Structure: jpayne@69: outer: Frame jpayne@69: buttons: Frame jpayne@69: (no assignment): Button (ok) jpayne@69: (no assignment): Button (apply) jpayne@69: (no assignment): Button (cancel) jpayne@69: (no assignment): Button (help) jpayne@69: (no assignment): Frame jpayne@69: """ jpayne@69: if macosx.isAquaTk(): jpayne@69: # Changing the default padding on OSX results in unreadable jpayne@69: # text in the buttons. jpayne@69: padding_args = {} jpayne@69: else: jpayne@69: padding_args = {'padding': (6, 3)} jpayne@69: outer = Frame(self, padding=2) jpayne@69: buttons = Frame(outer, padding=2) jpayne@69: for txt, cmd in ( jpayne@69: ('Ok', self.ok), jpayne@69: ('Apply', self.apply), jpayne@69: ('Cancel', self.cancel), jpayne@69: ('Help', self.help)): jpayne@69: Button(buttons, text=txt, command=cmd, takefocus=FALSE, jpayne@69: **padding_args).pack(side=LEFT, padx=5) jpayne@69: # Add space above buttons. jpayne@69: Frame(outer, height=2, borderwidth=0).pack(side=TOP) jpayne@69: buttons.pack(side=BOTTOM) jpayne@69: return outer jpayne@69: jpayne@69: def ok(self): jpayne@69: """Apply config changes, then dismiss dialog. jpayne@69: jpayne@69: Methods: jpayne@69: apply jpayne@69: destroy: inherited jpayne@69: """ jpayne@69: self.apply() jpayne@69: self.destroy() jpayne@69: jpayne@69: def apply(self): jpayne@69: """Apply config changes and leave dialog open. jpayne@69: jpayne@69: Methods: jpayne@69: deactivate_current_config jpayne@69: save_all_changed_extensions jpayne@69: activate_config_changes jpayne@69: """ jpayne@69: self.deactivate_current_config() jpayne@69: changes.save_all() jpayne@69: self.save_all_changed_extensions() jpayne@69: self.activate_config_changes() jpayne@69: jpayne@69: def cancel(self): jpayne@69: """Dismiss config dialog. jpayne@69: jpayne@69: Methods: jpayne@69: destroy: inherited jpayne@69: """ jpayne@69: self.destroy() jpayne@69: jpayne@69: def destroy(self): jpayne@69: global font_sample_text jpayne@69: font_sample_text = self.fontpage.font_sample.get('1.0', 'end') jpayne@69: self.grab_release() jpayne@69: super().destroy() jpayne@69: jpayne@69: def help(self): jpayne@69: """Create textview for config dialog help. jpayne@69: jpayne@69: Attributes accessed: jpayne@69: note jpayne@69: jpayne@69: Methods: jpayne@69: view_text: Method from textview module. jpayne@69: """ jpayne@69: page = self.note.tab(self.note.select(), option='text').strip() jpayne@69: view_text(self, title='Help for IDLE preferences', jpayne@69: text=help_common+help_pages.get(page, '')) jpayne@69: jpayne@69: def deactivate_current_config(self): jpayne@69: """Remove current key bindings. jpayne@69: Iterate over window instances defined in parent and remove jpayne@69: the keybindings. jpayne@69: """ jpayne@69: # Before a config is saved, some cleanup of current jpayne@69: # config must be done - remove the previous keybindings. jpayne@69: win_instances = self.parent.instance_dict.keys() jpayne@69: for instance in win_instances: jpayne@69: instance.RemoveKeybindings() jpayne@69: jpayne@69: def activate_config_changes(self): jpayne@69: """Apply configuration changes to current windows. jpayne@69: jpayne@69: Dynamically update the current parent window instances jpayne@69: with some of the configuration changes. jpayne@69: """ jpayne@69: win_instances = self.parent.instance_dict.keys() jpayne@69: for instance in win_instances: jpayne@69: instance.ResetColorizer() jpayne@69: instance.ResetFont() jpayne@69: instance.set_notabs_indentwidth() jpayne@69: instance.ApplyKeybindings() jpayne@69: instance.reset_help_menu_entries() jpayne@69: instance.update_cursor_blink() jpayne@69: for klass in reloadables: jpayne@69: klass.reload() jpayne@69: jpayne@69: def create_page_extensions(self): jpayne@69: """Part of the config dialog used for configuring IDLE extensions. jpayne@69: jpayne@69: This code is generic - it works for any and all IDLE extensions. jpayne@69: jpayne@69: IDLE extensions save their configuration options using idleConf. jpayne@69: This code reads the current configuration using idleConf, supplies a jpayne@69: GUI interface to change the configuration values, and saves the jpayne@69: changes using idleConf. jpayne@69: jpayne@69: Not all changes take effect immediately - some may require restarting IDLE. jpayne@69: This depends on each extension's implementation. jpayne@69: jpayne@69: All values are treated as text, and it is up to the user to supply jpayne@69: reasonable values. The only exception to this are the 'enable*' options, jpayne@69: which are boolean, and can be toggled with a True/False button. jpayne@69: jpayne@69: Methods: jpayne@69: load_extensions: jpayne@69: extension_selected: Handle selection from list. jpayne@69: create_extension_frame: Hold widgets for one extension. jpayne@69: set_extension_value: Set in userCfg['extensions']. jpayne@69: save_all_changed_extensions: Call extension page Save(). jpayne@69: """ jpayne@69: parent = self.parent jpayne@69: frame = Frame(self.note) jpayne@69: self.ext_defaultCfg = idleConf.defaultCfg['extensions'] jpayne@69: self.ext_userCfg = idleConf.userCfg['extensions'] jpayne@69: self.is_int = self.register(is_int) jpayne@69: self.load_extensions() jpayne@69: # Create widgets - a listbox shows all available extensions, with the jpayne@69: # controls for the extension selected in the listbox to the right. jpayne@69: self.extension_names = StringVar(self) jpayne@69: frame.rowconfigure(0, weight=1) jpayne@69: frame.columnconfigure(2, weight=1) jpayne@69: self.extension_list = Listbox(frame, listvariable=self.extension_names, jpayne@69: selectmode='browse') jpayne@69: self.extension_list.bind('<>', self.extension_selected) jpayne@69: scroll = Scrollbar(frame, command=self.extension_list.yview) jpayne@69: self.extension_list.yscrollcommand=scroll.set jpayne@69: self.details_frame = LabelFrame(frame, width=250, height=250) jpayne@69: self.extension_list.grid(column=0, row=0, sticky='nws') jpayne@69: scroll.grid(column=1, row=0, sticky='ns') jpayne@69: self.details_frame.grid(column=2, row=0, sticky='nsew', padx=[10, 0]) jpayne@69: frame.configure(padding=10) jpayne@69: self.config_frame = {} jpayne@69: self.current_extension = None jpayne@69: jpayne@69: self.outerframe = self # TEMPORARY jpayne@69: self.tabbed_page_set = self.extension_list # TEMPORARY jpayne@69: jpayne@69: # Create the frame holding controls for each extension. jpayne@69: ext_names = '' jpayne@69: for ext_name in sorted(self.extensions): jpayne@69: self.create_extension_frame(ext_name) jpayne@69: ext_names = ext_names + '{' + ext_name + '} ' jpayne@69: self.extension_names.set(ext_names) jpayne@69: self.extension_list.selection_set(0) jpayne@69: self.extension_selected(None) jpayne@69: jpayne@69: return frame jpayne@69: jpayne@69: def load_extensions(self): jpayne@69: "Fill self.extensions with data from the default and user configs." jpayne@69: self.extensions = {} jpayne@69: for ext_name in idleConf.GetExtensions(active_only=False): jpayne@69: # Former built-in extensions are already filtered out. jpayne@69: self.extensions[ext_name] = [] jpayne@69: jpayne@69: for ext_name in self.extensions: jpayne@69: opt_list = sorted(self.ext_defaultCfg.GetOptionList(ext_name)) jpayne@69: jpayne@69: # Bring 'enable' options to the beginning of the list. jpayne@69: enables = [opt_name for opt_name in opt_list jpayne@69: if opt_name.startswith('enable')] jpayne@69: for opt_name in enables: jpayne@69: opt_list.remove(opt_name) jpayne@69: opt_list = enables + opt_list jpayne@69: jpayne@69: for opt_name in opt_list: jpayne@69: def_str = self.ext_defaultCfg.Get( jpayne@69: ext_name, opt_name, raw=True) jpayne@69: try: jpayne@69: def_obj = {'True':True, 'False':False}[def_str] jpayne@69: opt_type = 'bool' jpayne@69: except KeyError: jpayne@69: try: jpayne@69: def_obj = int(def_str) jpayne@69: opt_type = 'int' jpayne@69: except ValueError: jpayne@69: def_obj = def_str jpayne@69: opt_type = None jpayne@69: try: jpayne@69: value = self.ext_userCfg.Get( jpayne@69: ext_name, opt_name, type=opt_type, raw=True, jpayne@69: default=def_obj) jpayne@69: except ValueError: # Need this until .Get fixed. jpayne@69: value = def_obj # Bad values overwritten by entry. jpayne@69: var = StringVar(self) jpayne@69: var.set(str(value)) jpayne@69: jpayne@69: self.extensions[ext_name].append({'name': opt_name, jpayne@69: 'type': opt_type, jpayne@69: 'default': def_str, jpayne@69: 'value': value, jpayne@69: 'var': var, jpayne@69: }) jpayne@69: jpayne@69: def extension_selected(self, event): jpayne@69: "Handle selection of an extension from the list." jpayne@69: newsel = self.extension_list.curselection() jpayne@69: if newsel: jpayne@69: newsel = self.extension_list.get(newsel) jpayne@69: if newsel is None or newsel != self.current_extension: jpayne@69: if self.current_extension: jpayne@69: self.details_frame.config(text='') jpayne@69: self.config_frame[self.current_extension].grid_forget() jpayne@69: self.current_extension = None jpayne@69: if newsel: jpayne@69: self.details_frame.config(text=newsel) jpayne@69: self.config_frame[newsel].grid(column=0, row=0, sticky='nsew') jpayne@69: self.current_extension = newsel jpayne@69: jpayne@69: def create_extension_frame(self, ext_name): jpayne@69: """Create a frame holding the widgets to configure one extension""" jpayne@69: f = VerticalScrolledFrame(self.details_frame, height=250, width=250) jpayne@69: self.config_frame[ext_name] = f jpayne@69: entry_area = f.interior jpayne@69: # Create an entry for each configuration option. jpayne@69: for row, opt in enumerate(self.extensions[ext_name]): jpayne@69: # Create a row with a label and entry/checkbutton. jpayne@69: label = Label(entry_area, text=opt['name']) jpayne@69: label.grid(row=row, column=0, sticky=NW) jpayne@69: var = opt['var'] jpayne@69: if opt['type'] == 'bool': jpayne@69: Checkbutton(entry_area, variable=var, jpayne@69: onvalue='True', offvalue='False', width=8 jpayne@69: ).grid(row=row, column=1, sticky=W, padx=7) jpayne@69: elif opt['type'] == 'int': jpayne@69: Entry(entry_area, textvariable=var, validate='key', jpayne@69: validatecommand=(self.is_int, '%P'), width=10 jpayne@69: ).grid(row=row, column=1, sticky=NSEW, padx=7) jpayne@69: jpayne@69: else: # type == 'str' jpayne@69: # Limit size to fit non-expanding space with larger font. jpayne@69: Entry(entry_area, textvariable=var, width=15 jpayne@69: ).grid(row=row, column=1, sticky=NSEW, padx=7) jpayne@69: return jpayne@69: jpayne@69: def set_extension_value(self, section, opt): jpayne@69: """Return True if the configuration was added or changed. jpayne@69: jpayne@69: If the value is the same as the default, then remove it jpayne@69: from user config file. jpayne@69: """ jpayne@69: name = opt['name'] jpayne@69: default = opt['default'] jpayne@69: value = opt['var'].get().strip() or default jpayne@69: opt['var'].set(value) jpayne@69: # if self.defaultCfg.has_section(section): jpayne@69: # Currently, always true; if not, indent to return. jpayne@69: if (value == default): jpayne@69: return self.ext_userCfg.RemoveOption(section, name) jpayne@69: # Set the option. jpayne@69: return self.ext_userCfg.SetOption(section, name, value) jpayne@69: jpayne@69: def save_all_changed_extensions(self): jpayne@69: """Save configuration changes to the user config file. jpayne@69: jpayne@69: Attributes accessed: jpayne@69: extensions jpayne@69: jpayne@69: Methods: jpayne@69: set_extension_value jpayne@69: """ jpayne@69: has_changes = False jpayne@69: for ext_name in self.extensions: jpayne@69: options = self.extensions[ext_name] jpayne@69: for opt in options: jpayne@69: if self.set_extension_value(ext_name, opt): jpayne@69: has_changes = True jpayne@69: if has_changes: jpayne@69: self.ext_userCfg.Save() jpayne@69: jpayne@69: jpayne@69: # class TabPage(Frame): # A template for Page classes. jpayne@69: # def __init__(self, master): jpayne@69: # super().__init__(master) jpayne@69: # self.create_page_tab() jpayne@69: # self.load_tab_cfg() jpayne@69: # def create_page_tab(self): jpayne@69: # # Define tk vars and register var and callback with tracers. jpayne@69: # # Create subframes and widgets. jpayne@69: # # Pack widgets. jpayne@69: # def load_tab_cfg(self): jpayne@69: # # Initialize widgets with data from idleConf. jpayne@69: # def var_changed_var_name(): jpayne@69: # # For each tk var that needs other than default callback. jpayne@69: # def other_methods(): jpayne@69: # # Define tab-specific behavior. jpayne@69: jpayne@69: font_sample_text = ( jpayne@69: '\n' jpayne@69: 'AaBbCcDdEeFfGgHhIiJj\n1234567890#:+=(){}[]\n' jpayne@69: '\u00a2\u00a3\u00a5\u00a7\u00a9\u00ab\u00ae\u00b6\u00bd\u011e' jpayne@69: '\u00c0\u00c1\u00c2\u00c3\u00c4\u00c5\u00c7\u00d0\u00d8\u00df\n' jpayne@69: '\n\n' jpayne@69: '\u0250\u0255\u0258\u025e\u025f\u0264\u026b\u026e\u0270\u0277' jpayne@69: '\u027b\u0281\u0283\u0286\u028e\u029e\u02a2\u02ab\u02ad\u02af\n' jpayne@69: '\u0391\u03b1\u0392\u03b2\u0393\u03b3\u0394\u03b4\u0395\u03b5' jpayne@69: '\u0396\u03b6\u0397\u03b7\u0398\u03b8\u0399\u03b9\u039a\u03ba\n' jpayne@69: '\u0411\u0431\u0414\u0434\u0416\u0436\u041f\u043f\u0424\u0444' jpayne@69: '\u0427\u0447\u042a\u044a\u042d\u044d\u0460\u0464\u046c\u04dc\n' jpayne@69: '\n\n' jpayne@69: '\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9' jpayne@69: '\u05da\u05db\u05dc\u05dd\u05de\u05df\u05e0\u05e1\u05e2\u05e3\n' jpayne@69: '\u0627\u0628\u062c\u062f\u0647\u0648\u0632\u062d\u0637\u064a' jpayne@69: '\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669\n' jpayne@69: '\n\n' jpayne@69: '\u0966\u0967\u0968\u0969\u096a\u096b\u096c\u096d\u096e\u096f' jpayne@69: '\u0905\u0906\u0907\u0908\u0909\u090a\u090f\u0910\u0913\u0914\n' jpayne@69: '\u0be6\u0be7\u0be8\u0be9\u0bea\u0beb\u0bec\u0bed\u0bee\u0bef' jpayne@69: '\u0b85\u0b87\u0b89\u0b8e\n' jpayne@69: '\n\n' jpayne@69: '\u3007\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\n' jpayne@69: '\u6c49\u5b57\u6f22\u5b57\u4eba\u6728\u706b\u571f\u91d1\u6c34\n' jpayne@69: '\uac00\ub0d0\ub354\ub824\ubaa8\ubd64\uc218\uc720\uc988\uce58\n' jpayne@69: '\u3042\u3044\u3046\u3048\u304a\u30a2\u30a4\u30a6\u30a8\u30aa\n' jpayne@69: ) jpayne@69: jpayne@69: jpayne@69: class FontPage(Frame): jpayne@69: jpayne@69: def __init__(self, master, highpage): jpayne@69: super().__init__(master) jpayne@69: self.highlight_sample = highpage.highlight_sample jpayne@69: self.create_page_font_tab() jpayne@69: self.load_font_cfg() jpayne@69: self.load_tab_cfg() jpayne@69: jpayne@69: def create_page_font_tab(self): jpayne@69: """Return frame of widgets for Font/Tabs tab. jpayne@69: jpayne@69: Fonts: Enable users to provisionally change font face, size, or jpayne@69: boldness and to see the consequence of proposed choices. Each jpayne@69: action set 3 options in changes structuree and changes the jpayne@69: corresponding aspect of the font sample on this page and jpayne@69: highlight sample on highlight page. jpayne@69: jpayne@69: Function load_font_cfg initializes font vars and widgets from jpayne@69: idleConf entries and tk. jpayne@69: jpayne@69: Fontlist: mouse button 1 click or up or down key invoke jpayne@69: on_fontlist_select(), which sets var font_name. jpayne@69: jpayne@69: Sizelist: clicking the menubutton opens the dropdown menu. A jpayne@69: mouse button 1 click or return key sets var font_size. jpayne@69: jpayne@69: Bold_toggle: clicking the box toggles var font_bold. jpayne@69: jpayne@69: Changing any of the font vars invokes var_changed_font, which jpayne@69: adds all 3 font options to changes and calls set_samples. jpayne@69: Set_samples applies a new font constructed from the font vars to jpayne@69: font_sample and to highlight_sample on the highlight page. jpayne@69: jpayne@69: Tabs: Enable users to change spaces entered for indent tabs. jpayne@69: Changing indent_scale value with the mouse sets Var space_num, jpayne@69: which invokes the default callback to add an entry to jpayne@69: changes. Load_tab_cfg initializes space_num to default. jpayne@69: jpayne@69: Widgets for FontPage(Frame): (*) widgets bound to self jpayne@69: frame_font: LabelFrame jpayne@69: frame_font_name: Frame jpayne@69: font_name_title: Label jpayne@69: (*)fontlist: ListBox - font_name jpayne@69: scroll_font: Scrollbar jpayne@69: frame_font_param: Frame jpayne@69: font_size_title: Label jpayne@69: (*)sizelist: DynOptionMenu - font_size jpayne@69: (*)bold_toggle: Checkbutton - font_bold jpayne@69: frame_sample: LabelFrame jpayne@69: (*)font_sample: Label jpayne@69: frame_indent: LabelFrame jpayne@69: indent_title: Label jpayne@69: (*)indent_scale: Scale - space_num jpayne@69: """ jpayne@69: self.font_name = tracers.add(StringVar(self), self.var_changed_font) jpayne@69: self.font_size = tracers.add(StringVar(self), self.var_changed_font) jpayne@69: self.font_bold = tracers.add(BooleanVar(self), self.var_changed_font) jpayne@69: self.space_num = tracers.add(IntVar(self), ('main', 'Indent', 'num-spaces')) jpayne@69: jpayne@69: # Define frames and widgets. jpayne@69: frame_font = LabelFrame( jpayne@69: self, borderwidth=2, relief=GROOVE, text=' Shell/Editor Font ') jpayne@69: frame_sample = LabelFrame( jpayne@69: self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Font Sample (Editable) ') jpayne@69: frame_indent = LabelFrame( jpayne@69: self, borderwidth=2, relief=GROOVE, text=' Indentation Width ') jpayne@69: # frame_font. jpayne@69: frame_font_name = Frame(frame_font) jpayne@69: frame_font_param = Frame(frame_font) jpayne@69: font_name_title = Label( jpayne@69: frame_font_name, justify=LEFT, text='Font Face :') jpayne@69: self.fontlist = Listbox(frame_font_name, height=15, jpayne@69: takefocus=True, exportselection=FALSE) jpayne@69: self.fontlist.bind('', self.on_fontlist_select) jpayne@69: self.fontlist.bind('', self.on_fontlist_select) jpayne@69: self.fontlist.bind('', self.on_fontlist_select) jpayne@69: scroll_font = Scrollbar(frame_font_name) jpayne@69: scroll_font.config(command=self.fontlist.yview) jpayne@69: self.fontlist.config(yscrollcommand=scroll_font.set) jpayne@69: font_size_title = Label(frame_font_param, text='Size :') jpayne@69: self.sizelist = DynOptionMenu(frame_font_param, self.font_size, None) jpayne@69: self.bold_toggle = Checkbutton( jpayne@69: frame_font_param, variable=self.font_bold, jpayne@69: onvalue=1, offvalue=0, text='Bold') jpayne@69: # frame_sample. jpayne@69: font_sample_frame = ScrollableTextFrame(frame_sample) jpayne@69: self.font_sample = font_sample_frame.text jpayne@69: self.font_sample.config(wrap=NONE, width=1, height=1) jpayne@69: self.font_sample.insert(END, font_sample_text) jpayne@69: # frame_indent. jpayne@69: indent_title = Label( jpayne@69: frame_indent, justify=LEFT, jpayne@69: text='Python Standard: 4 Spaces!') jpayne@69: self.indent_scale = Scale( jpayne@69: frame_indent, variable=self.space_num, jpayne@69: orient='horizontal', tickinterval=2, from_=2, to=16) jpayne@69: jpayne@69: # Grid and pack widgets: jpayne@69: self.columnconfigure(1, weight=1) jpayne@69: self.rowconfigure(2, weight=1) jpayne@69: frame_font.grid(row=0, column=0, padx=5, pady=5) jpayne@69: frame_sample.grid(row=0, column=1, rowspan=3, padx=5, pady=5, jpayne@69: sticky='nsew') jpayne@69: frame_indent.grid(row=1, column=0, padx=5, pady=5, sticky='ew') jpayne@69: # frame_font. jpayne@69: frame_font_name.pack(side=TOP, padx=5, pady=5, fill=X) jpayne@69: frame_font_param.pack(side=TOP, padx=5, pady=5, fill=X) jpayne@69: font_name_title.pack(side=TOP, anchor=W) jpayne@69: self.fontlist.pack(side=LEFT, expand=TRUE, fill=X) jpayne@69: scroll_font.pack(side=LEFT, fill=Y) jpayne@69: font_size_title.pack(side=LEFT, anchor=W) jpayne@69: self.sizelist.pack(side=LEFT, anchor=W) jpayne@69: self.bold_toggle.pack(side=LEFT, anchor=W, padx=20) jpayne@69: # frame_sample. jpayne@69: font_sample_frame.pack(expand=TRUE, fill=BOTH) jpayne@69: # frame_indent. jpayne@69: indent_title.pack(side=TOP, anchor=W, padx=5) jpayne@69: self.indent_scale.pack(side=TOP, padx=5, fill=X) jpayne@69: jpayne@69: def load_font_cfg(self): jpayne@69: """Load current configuration settings for the font options. jpayne@69: jpayne@69: Retrieve current font with idleConf.GetFont and font families jpayne@69: from tk. Setup fontlist and set font_name. Setup sizelist, jpayne@69: which sets font_size. Set font_bold. Call set_samples. jpayne@69: """ jpayne@69: configured_font = idleConf.GetFont(self, 'main', 'EditorWindow') jpayne@69: font_name = configured_font[0].lower() jpayne@69: font_size = configured_font[1] jpayne@69: font_bold = configured_font[2]=='bold' jpayne@69: jpayne@69: # Set editor font selection list and font_name. jpayne@69: fonts = list(tkFont.families(self)) jpayne@69: fonts.sort() jpayne@69: for font in fonts: jpayne@69: self.fontlist.insert(END, font) jpayne@69: self.font_name.set(font_name) jpayne@69: lc_fonts = [s.lower() for s in fonts] jpayne@69: try: jpayne@69: current_font_index = lc_fonts.index(font_name) jpayne@69: self.fontlist.see(current_font_index) jpayne@69: self.fontlist.select_set(current_font_index) jpayne@69: self.fontlist.select_anchor(current_font_index) jpayne@69: self.fontlist.activate(current_font_index) jpayne@69: except ValueError: jpayne@69: pass jpayne@69: # Set font size dropdown. jpayne@69: self.sizelist.SetMenu(('7', '8', '9', '10', '11', '12', '13', '14', jpayne@69: '16', '18', '20', '22', '25', '29', '34', '40'), jpayne@69: font_size) jpayne@69: # Set font weight. jpayne@69: self.font_bold.set(font_bold) jpayne@69: self.set_samples() jpayne@69: jpayne@69: def var_changed_font(self, *params): jpayne@69: """Store changes to font attributes. jpayne@69: jpayne@69: When one font attribute changes, save them all, as they are jpayne@69: not independent from each other. In particular, when we are jpayne@69: overriding the default font, we need to write out everything. jpayne@69: """ jpayne@69: value = self.font_name.get() jpayne@69: changes.add_option('main', 'EditorWindow', 'font', value) jpayne@69: value = self.font_size.get() jpayne@69: changes.add_option('main', 'EditorWindow', 'font-size', value) jpayne@69: value = self.font_bold.get() jpayne@69: changes.add_option('main', 'EditorWindow', 'font-bold', value) jpayne@69: self.set_samples() jpayne@69: jpayne@69: def on_fontlist_select(self, event): jpayne@69: """Handle selecting a font from the list. jpayne@69: jpayne@69: Event can result from either mouse click or Up or Down key. jpayne@69: Set font_name and example displays to selection. jpayne@69: """ jpayne@69: font = self.fontlist.get( jpayne@69: ACTIVE if event.type.name == 'KeyRelease' else ANCHOR) jpayne@69: self.font_name.set(font.lower()) jpayne@69: jpayne@69: def set_samples(self, event=None): jpayne@69: """Update update both screen samples with the font settings. jpayne@69: jpayne@69: Called on font initialization and change events. jpayne@69: Accesses font_name, font_size, and font_bold Variables. jpayne@69: Updates font_sample and highlight page highlight_sample. jpayne@69: """ jpayne@69: font_name = self.font_name.get() jpayne@69: font_weight = tkFont.BOLD if self.font_bold.get() else tkFont.NORMAL jpayne@69: new_font = (font_name, self.font_size.get(), font_weight) jpayne@69: self.font_sample['font'] = new_font jpayne@69: self.highlight_sample['font'] = new_font jpayne@69: jpayne@69: def load_tab_cfg(self): jpayne@69: """Load current configuration settings for the tab options. jpayne@69: jpayne@69: Attributes updated: jpayne@69: space_num: Set to value from idleConf. jpayne@69: """ jpayne@69: # Set indent sizes. jpayne@69: space_num = idleConf.GetOption( jpayne@69: 'main', 'Indent', 'num-spaces', default=4, type='int') jpayne@69: self.space_num.set(space_num) jpayne@69: jpayne@69: def var_changed_space_num(self, *params): jpayne@69: "Store change to indentation size." jpayne@69: value = self.space_num.get() jpayne@69: changes.add_option('main', 'Indent', 'num-spaces', value) jpayne@69: jpayne@69: jpayne@69: class HighPage(Frame): jpayne@69: jpayne@69: def __init__(self, master): jpayne@69: super().__init__(master) jpayne@69: self.cd = master.master jpayne@69: self.style = Style(master) jpayne@69: self.create_page_highlight() jpayne@69: self.load_theme_cfg() jpayne@69: jpayne@69: def create_page_highlight(self): jpayne@69: """Return frame of widgets for Highlighting tab. jpayne@69: jpayne@69: Enable users to provisionally change foreground and background jpayne@69: colors applied to textual tags. Color mappings are stored in jpayne@69: complete listings called themes. Built-in themes in jpayne@69: idlelib/config-highlight.def are fixed as far as the dialog is jpayne@69: concerned. Any theme can be used as the base for a new custom jpayne@69: theme, stored in .idlerc/config-highlight.cfg. jpayne@69: jpayne@69: Function load_theme_cfg() initializes tk variables and theme jpayne@69: lists and calls paint_theme_sample() and set_highlight_target() jpayne@69: for the current theme. Radiobuttons builtin_theme_on and jpayne@69: custom_theme_on toggle var theme_source, which controls if the jpayne@69: current set of colors are from a builtin or custom theme. jpayne@69: DynOptionMenus builtinlist and customlist contain lists of the jpayne@69: builtin and custom themes, respectively, and the current item jpayne@69: from each list is stored in vars builtin_name and custom_name. jpayne@69: jpayne@69: Function paint_theme_sample() applies the colors from the theme jpayne@69: to the tags in text widget highlight_sample and then invokes jpayne@69: set_color_sample(). Function set_highlight_target() sets the state jpayne@69: of the radiobuttons fg_on and bg_on based on the tag and it also jpayne@69: invokes set_color_sample(). jpayne@69: jpayne@69: Function set_color_sample() sets the background color for the frame jpayne@69: holding the color selector. This provides a larger visual of the jpayne@69: color for the current tag and plane (foreground/background). jpayne@69: jpayne@69: Note: set_color_sample() is called from many places and is often jpayne@69: called more than once when a change is made. It is invoked when jpayne@69: foreground or background is selected (radiobuttons), from jpayne@69: paint_theme_sample() (theme is changed or load_cfg is called), and jpayne@69: from set_highlight_target() (target tag is changed or load_cfg called). jpayne@69: jpayne@69: Button delete_custom invokes delete_custom() to delete jpayne@69: a custom theme from idleConf.userCfg['highlight'] and changes. jpayne@69: Button save_custom invokes save_as_new_theme() which calls jpayne@69: get_new_theme_name() and create_new() to save a custom theme jpayne@69: and its colors to idleConf.userCfg['highlight']. jpayne@69: jpayne@69: Radiobuttons fg_on and bg_on toggle var fg_bg_toggle to control jpayne@69: if the current selected color for a tag is for the foreground or jpayne@69: background. jpayne@69: jpayne@69: DynOptionMenu targetlist contains a readable description of the jpayne@69: tags applied to Python source within IDLE. Selecting one of the jpayne@69: tags from this list populates highlight_target, which has a callback jpayne@69: function set_highlight_target(). jpayne@69: jpayne@69: Text widget highlight_sample displays a block of text (which is jpayne@69: mock Python code) in which is embedded the defined tags and reflects jpayne@69: the color attributes of the current theme and changes for those tags. jpayne@69: Mouse button 1 allows for selection of a tag and updates jpayne@69: highlight_target with that tag value. jpayne@69: jpayne@69: Note: The font in highlight_sample is set through the config in jpayne@69: the fonts tab. jpayne@69: jpayne@69: In other words, a tag can be selected either from targetlist or jpayne@69: by clicking on the sample text within highlight_sample. The jpayne@69: plane (foreground/background) is selected via the radiobutton. jpayne@69: Together, these two (tag and plane) control what color is jpayne@69: shown in set_color_sample() for the current theme. Button set_color jpayne@69: invokes get_color() which displays a ColorChooser to change the jpayne@69: color for the selected tag/plane. If a new color is picked, jpayne@69: it will be saved to changes and the highlight_sample and jpayne@69: frame background will be updated. jpayne@69: jpayne@69: Tk Variables: jpayne@69: color: Color of selected target. jpayne@69: builtin_name: Menu variable for built-in theme. jpayne@69: custom_name: Menu variable for custom theme. jpayne@69: fg_bg_toggle: Toggle for foreground/background color. jpayne@69: Note: this has no callback. jpayne@69: theme_source: Selector for built-in or custom theme. jpayne@69: highlight_target: Menu variable for the highlight tag target. jpayne@69: jpayne@69: Instance Data Attributes: jpayne@69: theme_elements: Dictionary of tags for text highlighting. jpayne@69: The key is the display name and the value is a tuple of jpayne@69: (tag name, display sort order). jpayne@69: jpayne@69: Methods [attachment]: jpayne@69: load_theme_cfg: Load current highlight colors. jpayne@69: get_color: Invoke colorchooser [button_set_color]. jpayne@69: set_color_sample_binding: Call set_color_sample [fg_bg_toggle]. jpayne@69: set_highlight_target: set fg_bg_toggle, set_color_sample(). jpayne@69: set_color_sample: Set frame background to target. jpayne@69: on_new_color_set: Set new color and add option. jpayne@69: paint_theme_sample: Recolor sample. jpayne@69: get_new_theme_name: Get from popup. jpayne@69: create_new: Combine theme with changes and save. jpayne@69: save_as_new_theme: Save [button_save_custom]. jpayne@69: set_theme_type: Command for [theme_source]. jpayne@69: delete_custom: Activate default [button_delete_custom]. jpayne@69: save_new: Save to userCfg['theme'] (is function). jpayne@69: jpayne@69: Widgets of highlights page frame: (*) widgets bound to self jpayne@69: frame_custom: LabelFrame jpayne@69: (*)highlight_sample: Text jpayne@69: (*)frame_color_set: Frame jpayne@69: (*)button_set_color: Button jpayne@69: (*)targetlist: DynOptionMenu - highlight_target jpayne@69: frame_fg_bg_toggle: Frame jpayne@69: (*)fg_on: Radiobutton - fg_bg_toggle jpayne@69: (*)bg_on: Radiobutton - fg_bg_toggle jpayne@69: (*)button_save_custom: Button jpayne@69: frame_theme: LabelFrame jpayne@69: theme_type_title: Label jpayne@69: (*)builtin_theme_on: Radiobutton - theme_source jpayne@69: (*)custom_theme_on: Radiobutton - theme_source jpayne@69: (*)builtinlist: DynOptionMenu - builtin_name jpayne@69: (*)customlist: DynOptionMenu - custom_name jpayne@69: (*)button_delete_custom: Button jpayne@69: (*)theme_message: Label jpayne@69: """ jpayne@69: self.theme_elements = { jpayne@69: 'Normal Code or Text': ('normal', '00'), jpayne@69: 'Code Context': ('context', '01'), jpayne@69: 'Python Keywords': ('keyword', '02'), jpayne@69: 'Python Definitions': ('definition', '03'), jpayne@69: 'Python Builtins': ('builtin', '04'), jpayne@69: 'Python Comments': ('comment', '05'), jpayne@69: 'Python Strings': ('string', '06'), jpayne@69: 'Selected Text': ('hilite', '07'), jpayne@69: 'Found Text': ('hit', '08'), jpayne@69: 'Cursor': ('cursor', '09'), jpayne@69: 'Editor Breakpoint': ('break', '10'), jpayne@69: 'Shell Prompt': ('console', '11'), jpayne@69: 'Error Text': ('error', '12'), jpayne@69: 'Shell User Output': ('stdout', '13'), jpayne@69: 'Shell User Exception': ('stderr', '14'), jpayne@69: 'Line Number': ('linenumber', '16'), jpayne@69: } jpayne@69: self.builtin_name = tracers.add( jpayne@69: StringVar(self), self.var_changed_builtin_name) jpayne@69: self.custom_name = tracers.add( jpayne@69: StringVar(self), self.var_changed_custom_name) jpayne@69: self.fg_bg_toggle = BooleanVar(self) jpayne@69: self.color = tracers.add( jpayne@69: StringVar(self), self.var_changed_color) jpayne@69: self.theme_source = tracers.add( jpayne@69: BooleanVar(self), self.var_changed_theme_source) jpayne@69: self.highlight_target = tracers.add( jpayne@69: StringVar(self), self.var_changed_highlight_target) jpayne@69: jpayne@69: # Create widgets: jpayne@69: # body frame and section frames. jpayne@69: frame_custom = LabelFrame(self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Custom Highlighting ') jpayne@69: frame_theme = LabelFrame(self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Highlighting Theme ') jpayne@69: # frame_custom. jpayne@69: sample_frame = ScrollableTextFrame( jpayne@69: frame_custom, relief=SOLID, borderwidth=1) jpayne@69: text = self.highlight_sample = sample_frame.text jpayne@69: text.configure( jpayne@69: font=('courier', 12, ''), cursor='hand2', width=1, height=1, jpayne@69: takefocus=FALSE, highlightthickness=0, wrap=NONE) jpayne@69: text.bind('', lambda e: 'break') jpayne@69: text.bind('', lambda e: 'break') jpayne@69: string_tags=( jpayne@69: ('# Click selects item.', 'comment'), ('\n', 'normal'), jpayne@69: ('code context section', 'context'), ('\n', 'normal'), jpayne@69: ('| cursor', 'cursor'), ('\n', 'normal'), jpayne@69: ('def', 'keyword'), (' ', 'normal'), jpayne@69: ('func', 'definition'), ('(param):\n ', 'normal'), jpayne@69: ('"Return None."', 'string'), ('\n var0 = ', 'normal'), jpayne@69: ("'string'", 'string'), ('\n var1 = ', 'normal'), jpayne@69: ("'selected'", 'hilite'), ('\n var2 = ', 'normal'), jpayne@69: ("'found'", 'hit'), ('\n var3 = ', 'normal'), jpayne@69: ('list', 'builtin'), ('(', 'normal'), jpayne@69: ('None', 'keyword'), (')\n', 'normal'), jpayne@69: (' breakpoint("line")', 'break'), ('\n\n', 'normal'), jpayne@69: ('>>>', 'console'), (' 3.14**2\n', 'normal'), jpayne@69: ('9.8596', 'stdout'), ('\n', 'normal'), jpayne@69: ('>>>', 'console'), (' pri ', 'normal'), jpayne@69: ('n', 'error'), ('t(\n', 'normal'), jpayne@69: ('SyntaxError', 'stderr'), ('\n', 'normal')) jpayne@69: for string, tag in string_tags: jpayne@69: text.insert(END, string, tag) jpayne@69: n_lines = len(text.get('1.0', END).splitlines()) jpayne@69: for lineno in range(1, n_lines): jpayne@69: text.insert(f'{lineno}.0', jpayne@69: f'{lineno:{len(str(n_lines))}d} ', jpayne@69: 'linenumber') jpayne@69: for element in self.theme_elements: jpayne@69: def tem(event, elem=element): jpayne@69: # event.widget.winfo_top_level().highlight_target.set(elem) jpayne@69: self.highlight_target.set(elem) jpayne@69: text.tag_bind( jpayne@69: self.theme_elements[element][0], '', tem) jpayne@69: text['state'] = 'disabled' jpayne@69: self.style.configure('frame_color_set.TFrame', borderwidth=1, jpayne@69: relief='solid') jpayne@69: self.frame_color_set = Frame(frame_custom, style='frame_color_set.TFrame') jpayne@69: frame_fg_bg_toggle = Frame(frame_custom) jpayne@69: self.button_set_color = Button( jpayne@69: self.frame_color_set, text='Choose Color for :', jpayne@69: command=self.get_color) jpayne@69: self.targetlist = DynOptionMenu( jpayne@69: self.frame_color_set, self.highlight_target, None, jpayne@69: highlightthickness=0) #, command=self.set_highlight_targetBinding jpayne@69: self.fg_on = Radiobutton( jpayne@69: frame_fg_bg_toggle, variable=self.fg_bg_toggle, value=1, jpayne@69: text='Foreground', command=self.set_color_sample_binding) jpayne@69: self.bg_on = Radiobutton( jpayne@69: frame_fg_bg_toggle, variable=self.fg_bg_toggle, value=0, jpayne@69: text='Background', command=self.set_color_sample_binding) jpayne@69: self.fg_bg_toggle.set(1) jpayne@69: self.button_save_custom = Button( jpayne@69: frame_custom, text='Save as New Custom Theme', jpayne@69: command=self.save_as_new_theme) jpayne@69: # frame_theme. jpayne@69: theme_type_title = Label(frame_theme, text='Select : ') jpayne@69: self.builtin_theme_on = Radiobutton( jpayne@69: frame_theme, variable=self.theme_source, value=1, jpayne@69: command=self.set_theme_type, text='a Built-in Theme') jpayne@69: self.custom_theme_on = Radiobutton( jpayne@69: frame_theme, variable=self.theme_source, value=0, jpayne@69: command=self.set_theme_type, text='a Custom Theme') jpayne@69: self.builtinlist = DynOptionMenu( jpayne@69: frame_theme, self.builtin_name, None, command=None) jpayne@69: self.customlist = DynOptionMenu( jpayne@69: frame_theme, self.custom_name, None, command=None) jpayne@69: self.button_delete_custom = Button( jpayne@69: frame_theme, text='Delete Custom Theme', jpayne@69: command=self.delete_custom) jpayne@69: self.theme_message = Label(frame_theme, borderwidth=2) jpayne@69: # Pack widgets: jpayne@69: # body. jpayne@69: frame_custom.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: frame_theme.pack(side=TOP, padx=5, pady=5, fill=X) jpayne@69: # frame_custom. jpayne@69: self.frame_color_set.pack(side=TOP, padx=5, pady=5, fill=X) jpayne@69: frame_fg_bg_toggle.pack(side=TOP, padx=5, pady=0) jpayne@69: sample_frame.pack( jpayne@69: side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: self.button_set_color.pack(side=TOP, expand=TRUE, fill=X, padx=8, pady=4) jpayne@69: self.targetlist.pack(side=TOP, expand=TRUE, fill=X, padx=8, pady=3) jpayne@69: self.fg_on.pack(side=LEFT, anchor=E) jpayne@69: self.bg_on.pack(side=RIGHT, anchor=W) jpayne@69: self.button_save_custom.pack(side=BOTTOM, fill=X, padx=5, pady=5) jpayne@69: # frame_theme. jpayne@69: theme_type_title.pack(side=TOP, anchor=W, padx=5, pady=5) jpayne@69: self.builtin_theme_on.pack(side=TOP, anchor=W, padx=5) jpayne@69: self.custom_theme_on.pack(side=TOP, anchor=W, padx=5, pady=2) jpayne@69: self.builtinlist.pack(side=TOP, fill=X, padx=5, pady=5) jpayne@69: self.customlist.pack(side=TOP, fill=X, anchor=W, padx=5, pady=5) jpayne@69: self.button_delete_custom.pack(side=TOP, fill=X, padx=5, pady=5) jpayne@69: self.theme_message.pack(side=TOP, fill=X, pady=5) jpayne@69: jpayne@69: def load_theme_cfg(self): jpayne@69: """Load current configuration settings for the theme options. jpayne@69: jpayne@69: Based on the theme_source toggle, the theme is set as jpayne@69: either builtin or custom and the initial widget values jpayne@69: reflect the current settings from idleConf. jpayne@69: jpayne@69: Attributes updated: jpayne@69: theme_source: Set from idleConf. jpayne@69: builtinlist: List of default themes from idleConf. jpayne@69: customlist: List of custom themes from idleConf. jpayne@69: custom_theme_on: Disabled if there are no custom themes. jpayne@69: custom_theme: Message with additional information. jpayne@69: targetlist: Create menu from self.theme_elements. jpayne@69: jpayne@69: Methods: jpayne@69: set_theme_type jpayne@69: paint_theme_sample jpayne@69: set_highlight_target jpayne@69: """ jpayne@69: # Set current theme type radiobutton. jpayne@69: self.theme_source.set(idleConf.GetOption( jpayne@69: 'main', 'Theme', 'default', type='bool', default=1)) jpayne@69: # Set current theme. jpayne@69: current_option = idleConf.CurrentTheme() jpayne@69: # Load available theme option menus. jpayne@69: if self.theme_source.get(): # Default theme selected. jpayne@69: item_list = idleConf.GetSectionList('default', 'highlight') jpayne@69: item_list.sort() jpayne@69: self.builtinlist.SetMenu(item_list, current_option) jpayne@69: item_list = idleConf.GetSectionList('user', 'highlight') jpayne@69: item_list.sort() jpayne@69: if not item_list: jpayne@69: self.custom_theme_on.state(('disabled',)) jpayne@69: self.custom_name.set('- no custom themes -') jpayne@69: else: jpayne@69: self.customlist.SetMenu(item_list, item_list[0]) jpayne@69: else: # User theme selected. jpayne@69: item_list = idleConf.GetSectionList('user', 'highlight') jpayne@69: item_list.sort() jpayne@69: self.customlist.SetMenu(item_list, current_option) jpayne@69: item_list = idleConf.GetSectionList('default', 'highlight') jpayne@69: item_list.sort() jpayne@69: self.builtinlist.SetMenu(item_list, item_list[0]) jpayne@69: self.set_theme_type() jpayne@69: # Load theme element option menu. jpayne@69: theme_names = list(self.theme_elements.keys()) jpayne@69: theme_names.sort(key=lambda x: self.theme_elements[x][1]) jpayne@69: self.targetlist.SetMenu(theme_names, theme_names[0]) jpayne@69: self.paint_theme_sample() jpayne@69: self.set_highlight_target() jpayne@69: jpayne@69: def var_changed_builtin_name(self, *params): jpayne@69: """Process new builtin theme selection. jpayne@69: jpayne@69: Add the changed theme's name to the changed_items and recreate jpayne@69: the sample with the values from the selected theme. jpayne@69: """ jpayne@69: old_themes = ('IDLE Classic', 'IDLE New') jpayne@69: value = self.builtin_name.get() jpayne@69: if value not in old_themes: jpayne@69: if idleConf.GetOption('main', 'Theme', 'name') not in old_themes: jpayne@69: changes.add_option('main', 'Theme', 'name', old_themes[0]) jpayne@69: changes.add_option('main', 'Theme', 'name2', value) jpayne@69: self.theme_message['text'] = 'New theme, see Help' jpayne@69: else: jpayne@69: changes.add_option('main', 'Theme', 'name', value) jpayne@69: changes.add_option('main', 'Theme', 'name2', '') jpayne@69: self.theme_message['text'] = '' jpayne@69: self.paint_theme_sample() jpayne@69: jpayne@69: def var_changed_custom_name(self, *params): jpayne@69: """Process new custom theme selection. jpayne@69: jpayne@69: If a new custom theme is selected, add the name to the jpayne@69: changed_items and apply the theme to the sample. jpayne@69: """ jpayne@69: value = self.custom_name.get() jpayne@69: if value != '- no custom themes -': jpayne@69: changes.add_option('main', 'Theme', 'name', value) jpayne@69: self.paint_theme_sample() jpayne@69: jpayne@69: def var_changed_theme_source(self, *params): jpayne@69: """Process toggle between builtin and custom theme. jpayne@69: jpayne@69: Update the default toggle value and apply the newly jpayne@69: selected theme type. jpayne@69: """ jpayne@69: value = self.theme_source.get() jpayne@69: changes.add_option('main', 'Theme', 'default', value) jpayne@69: if value: jpayne@69: self.var_changed_builtin_name() jpayne@69: else: jpayne@69: self.var_changed_custom_name() jpayne@69: jpayne@69: def var_changed_color(self, *params): jpayne@69: "Process change to color choice." jpayne@69: self.on_new_color_set() jpayne@69: jpayne@69: def var_changed_highlight_target(self, *params): jpayne@69: "Process selection of new target tag for highlighting." jpayne@69: self.set_highlight_target() jpayne@69: jpayne@69: def set_theme_type(self): jpayne@69: """Set available screen options based on builtin or custom theme. jpayne@69: jpayne@69: Attributes accessed: jpayne@69: theme_source jpayne@69: jpayne@69: Attributes updated: jpayne@69: builtinlist jpayne@69: customlist jpayne@69: button_delete_custom jpayne@69: custom_theme_on jpayne@69: jpayne@69: Called from: jpayne@69: handler for builtin_theme_on and custom_theme_on jpayne@69: delete_custom jpayne@69: create_new jpayne@69: load_theme_cfg jpayne@69: """ jpayne@69: if self.theme_source.get(): jpayne@69: self.builtinlist['state'] = 'normal' jpayne@69: self.customlist['state'] = 'disabled' jpayne@69: self.button_delete_custom.state(('disabled',)) jpayne@69: else: jpayne@69: self.builtinlist['state'] = 'disabled' jpayne@69: self.custom_theme_on.state(('!disabled',)) jpayne@69: self.customlist['state'] = 'normal' jpayne@69: self.button_delete_custom.state(('!disabled',)) jpayne@69: jpayne@69: def get_color(self): jpayne@69: """Handle button to select a new color for the target tag. jpayne@69: jpayne@69: If a new color is selected while using a builtin theme, a jpayne@69: name must be supplied to create a custom theme. jpayne@69: jpayne@69: Attributes accessed: jpayne@69: highlight_target jpayne@69: frame_color_set jpayne@69: theme_source jpayne@69: jpayne@69: Attributes updated: jpayne@69: color jpayne@69: jpayne@69: Methods: jpayne@69: get_new_theme_name jpayne@69: create_new jpayne@69: """ jpayne@69: target = self.highlight_target.get() jpayne@69: prev_color = self.style.lookup(self.frame_color_set['style'], jpayne@69: 'background') jpayne@69: rgbTuplet, color_string = tkColorChooser.askcolor( jpayne@69: parent=self, title='Pick new color for : '+target, jpayne@69: initialcolor=prev_color) jpayne@69: if color_string and (color_string != prev_color): jpayne@69: # User didn't cancel and they chose a new color. jpayne@69: if self.theme_source.get(): # Current theme is a built-in. jpayne@69: message = ('Your changes will be saved as a new Custom Theme. ' jpayne@69: 'Enter a name for your new Custom Theme below.') jpayne@69: new_theme = self.get_new_theme_name(message) jpayne@69: if not new_theme: # User cancelled custom theme creation. jpayne@69: return jpayne@69: else: # Create new custom theme based on previously active theme. jpayne@69: self.create_new(new_theme) jpayne@69: self.color.set(color_string) jpayne@69: else: # Current theme is user defined. jpayne@69: self.color.set(color_string) jpayne@69: jpayne@69: def on_new_color_set(self): jpayne@69: "Display sample of new color selection on the dialog." jpayne@69: new_color = self.color.get() jpayne@69: self.style.configure('frame_color_set.TFrame', background=new_color) jpayne@69: plane = 'foreground' if self.fg_bg_toggle.get() else 'background' jpayne@69: sample_element = self.theme_elements[self.highlight_target.get()][0] jpayne@69: self.highlight_sample.tag_config(sample_element, **{plane: new_color}) jpayne@69: theme = self.custom_name.get() jpayne@69: theme_element = sample_element + '-' + plane jpayne@69: changes.add_option('highlight', theme, theme_element, new_color) jpayne@69: jpayne@69: def get_new_theme_name(self, message): jpayne@69: "Return name of new theme from query popup." jpayne@69: used_names = (idleConf.GetSectionList('user', 'highlight') + jpayne@69: idleConf.GetSectionList('default', 'highlight')) jpayne@69: new_theme = SectionName( jpayne@69: self, 'New Custom Theme', message, used_names).result jpayne@69: return new_theme jpayne@69: jpayne@69: def save_as_new_theme(self): jpayne@69: """Prompt for new theme name and create the theme. jpayne@69: jpayne@69: Methods: jpayne@69: get_new_theme_name jpayne@69: create_new jpayne@69: """ jpayne@69: new_theme_name = self.get_new_theme_name('New Theme Name:') jpayne@69: if new_theme_name: jpayne@69: self.create_new(new_theme_name) jpayne@69: jpayne@69: def create_new(self, new_theme_name): jpayne@69: """Create a new custom theme with the given name. jpayne@69: jpayne@69: Create the new theme based on the previously active theme jpayne@69: with the current changes applied. Once it is saved, then jpayne@69: activate the new theme. jpayne@69: jpayne@69: Attributes accessed: jpayne@69: builtin_name jpayne@69: custom_name jpayne@69: jpayne@69: Attributes updated: jpayne@69: customlist jpayne@69: theme_source jpayne@69: jpayne@69: Method: jpayne@69: save_new jpayne@69: set_theme_type jpayne@69: """ jpayne@69: if self.theme_source.get(): jpayne@69: theme_type = 'default' jpayne@69: theme_name = self.builtin_name.get() jpayne@69: else: jpayne@69: theme_type = 'user' jpayne@69: theme_name = self.custom_name.get() jpayne@69: new_theme = idleConf.GetThemeDict(theme_type, theme_name) jpayne@69: # Apply any of the old theme's unsaved changes to the new theme. jpayne@69: if theme_name in changes['highlight']: jpayne@69: theme_changes = changes['highlight'][theme_name] jpayne@69: for element in theme_changes: jpayne@69: new_theme[element] = theme_changes[element] jpayne@69: # Save the new theme. jpayne@69: self.save_new(new_theme_name, new_theme) jpayne@69: # Change GUI over to the new theme. jpayne@69: custom_theme_list = idleConf.GetSectionList('user', 'highlight') jpayne@69: custom_theme_list.sort() jpayne@69: self.customlist.SetMenu(custom_theme_list, new_theme_name) jpayne@69: self.theme_source.set(0) jpayne@69: self.set_theme_type() jpayne@69: jpayne@69: def set_highlight_target(self): jpayne@69: """Set fg/bg toggle and color based on highlight tag target. jpayne@69: jpayne@69: Instance variables accessed: jpayne@69: highlight_target jpayne@69: jpayne@69: Attributes updated: jpayne@69: fg_on jpayne@69: bg_on jpayne@69: fg_bg_toggle jpayne@69: jpayne@69: Methods: jpayne@69: set_color_sample jpayne@69: jpayne@69: Called from: jpayne@69: var_changed_highlight_target jpayne@69: load_theme_cfg jpayne@69: """ jpayne@69: if self.highlight_target.get() == 'Cursor': # bg not possible jpayne@69: self.fg_on.state(('disabled',)) jpayne@69: self.bg_on.state(('disabled',)) jpayne@69: self.fg_bg_toggle.set(1) jpayne@69: else: # Both fg and bg can be set. jpayne@69: self.fg_on.state(('!disabled',)) jpayne@69: self.bg_on.state(('!disabled',)) jpayne@69: self.fg_bg_toggle.set(1) jpayne@69: self.set_color_sample() jpayne@69: jpayne@69: def set_color_sample_binding(self, *args): jpayne@69: """Change color sample based on foreground/background toggle. jpayne@69: jpayne@69: Methods: jpayne@69: set_color_sample jpayne@69: """ jpayne@69: self.set_color_sample() jpayne@69: jpayne@69: def set_color_sample(self): jpayne@69: """Set the color of the frame background to reflect the selected target. jpayne@69: jpayne@69: Instance variables accessed: jpayne@69: theme_elements jpayne@69: highlight_target jpayne@69: fg_bg_toggle jpayne@69: highlight_sample jpayne@69: jpayne@69: Attributes updated: jpayne@69: frame_color_set jpayne@69: """ jpayne@69: # Set the color sample area. jpayne@69: tag = self.theme_elements[self.highlight_target.get()][0] jpayne@69: plane = 'foreground' if self.fg_bg_toggle.get() else 'background' jpayne@69: color = self.highlight_sample.tag_cget(tag, plane) jpayne@69: self.style.configure('frame_color_set.TFrame', background=color) jpayne@69: jpayne@69: def paint_theme_sample(self): jpayne@69: """Apply the theme colors to each element tag in the sample text. jpayne@69: jpayne@69: Instance attributes accessed: jpayne@69: theme_elements jpayne@69: theme_source jpayne@69: builtin_name jpayne@69: custom_name jpayne@69: jpayne@69: Attributes updated: jpayne@69: highlight_sample: Set the tag elements to the theme. jpayne@69: jpayne@69: Methods: jpayne@69: set_color_sample jpayne@69: jpayne@69: Called from: jpayne@69: var_changed_builtin_name jpayne@69: var_changed_custom_name jpayne@69: load_theme_cfg jpayne@69: """ jpayne@69: if self.theme_source.get(): # Default theme jpayne@69: theme = self.builtin_name.get() jpayne@69: else: # User theme jpayne@69: theme = self.custom_name.get() jpayne@69: for element_title in self.theme_elements: jpayne@69: element = self.theme_elements[element_title][0] jpayne@69: colors = idleConf.GetHighlight(theme, element) jpayne@69: if element == 'cursor': # Cursor sample needs special painting. jpayne@69: colors['background'] = idleConf.GetHighlight( jpayne@69: theme, 'normal')['background'] jpayne@69: # Handle any unsaved changes to this theme. jpayne@69: if theme in changes['highlight']: jpayne@69: theme_dict = changes['highlight'][theme] jpayne@69: if element + '-foreground' in theme_dict: jpayne@69: colors['foreground'] = theme_dict[element + '-foreground'] jpayne@69: if element + '-background' in theme_dict: jpayne@69: colors['background'] = theme_dict[element + '-background'] jpayne@69: self.highlight_sample.tag_config(element, **colors) jpayne@69: self.set_color_sample() jpayne@69: jpayne@69: def save_new(self, theme_name, theme): jpayne@69: """Save a newly created theme to idleConf. jpayne@69: jpayne@69: theme_name - string, the name of the new theme jpayne@69: theme - dictionary containing the new theme jpayne@69: """ jpayne@69: if not idleConf.userCfg['highlight'].has_section(theme_name): jpayne@69: idleConf.userCfg['highlight'].add_section(theme_name) jpayne@69: for element in theme: jpayne@69: value = theme[element] jpayne@69: idleConf.userCfg['highlight'].SetOption(theme_name, element, value) jpayne@69: jpayne@69: def askyesno(self, *args, **kwargs): jpayne@69: # Make testing easier. Could change implementation. jpayne@69: return messagebox.askyesno(*args, **kwargs) jpayne@69: jpayne@69: def delete_custom(self): jpayne@69: """Handle event to delete custom theme. jpayne@69: jpayne@69: The current theme is deactivated and the default theme is jpayne@69: activated. The custom theme is permanently removed from jpayne@69: the config file. jpayne@69: jpayne@69: Attributes accessed: jpayne@69: custom_name jpayne@69: jpayne@69: Attributes updated: jpayne@69: custom_theme_on jpayne@69: customlist jpayne@69: theme_source jpayne@69: builtin_name jpayne@69: jpayne@69: Methods: jpayne@69: deactivate_current_config jpayne@69: save_all_changed_extensions jpayne@69: activate_config_changes jpayne@69: set_theme_type jpayne@69: """ jpayne@69: theme_name = self.custom_name.get() jpayne@69: delmsg = 'Are you sure you wish to delete the theme %r ?' jpayne@69: if not self.askyesno( jpayne@69: 'Delete Theme', delmsg % theme_name, parent=self): jpayne@69: return jpayne@69: self.cd.deactivate_current_config() jpayne@69: # Remove theme from changes, config, and file. jpayne@69: changes.delete_section('highlight', theme_name) jpayne@69: # Reload user theme list. jpayne@69: item_list = idleConf.GetSectionList('user', 'highlight') jpayne@69: item_list.sort() jpayne@69: if not item_list: jpayne@69: self.custom_theme_on.state(('disabled',)) jpayne@69: self.customlist.SetMenu(item_list, '- no custom themes -') jpayne@69: else: jpayne@69: self.customlist.SetMenu(item_list, item_list[0]) jpayne@69: # Revert to default theme. jpayne@69: self.theme_source.set(idleConf.defaultCfg['main'].Get('Theme', 'default')) jpayne@69: self.builtin_name.set(idleConf.defaultCfg['main'].Get('Theme', 'name')) jpayne@69: # User can't back out of these changes, they must be applied now. jpayne@69: changes.save_all() jpayne@69: self.cd.save_all_changed_extensions() jpayne@69: self.cd.activate_config_changes() jpayne@69: self.set_theme_type() jpayne@69: jpayne@69: jpayne@69: class KeysPage(Frame): jpayne@69: jpayne@69: def __init__(self, master): jpayne@69: super().__init__(master) jpayne@69: self.cd = master.master jpayne@69: self.create_page_keys() jpayne@69: self.load_key_cfg() jpayne@69: jpayne@69: def create_page_keys(self): jpayne@69: """Return frame of widgets for Keys tab. jpayne@69: jpayne@69: Enable users to provisionally change both individual and sets of jpayne@69: keybindings (shortcut keys). Except for features implemented as jpayne@69: extensions, keybindings are stored in complete sets called jpayne@69: keysets. Built-in keysets in idlelib/config-keys.def are fixed jpayne@69: as far as the dialog is concerned. Any keyset can be used as the jpayne@69: base for a new custom keyset, stored in .idlerc/config-keys.cfg. jpayne@69: jpayne@69: Function load_key_cfg() initializes tk variables and keyset jpayne@69: lists and calls load_keys_list for the current keyset. jpayne@69: Radiobuttons builtin_keyset_on and custom_keyset_on toggle var jpayne@69: keyset_source, which controls if the current set of keybindings jpayne@69: are from a builtin or custom keyset. DynOptionMenus builtinlist jpayne@69: and customlist contain lists of the builtin and custom keysets, jpayne@69: respectively, and the current item from each list is stored in jpayne@69: vars builtin_name and custom_name. jpayne@69: jpayne@69: Button delete_custom_keys invokes delete_custom_keys() to delete jpayne@69: a custom keyset from idleConf.userCfg['keys'] and changes. Button jpayne@69: save_custom_keys invokes save_as_new_key_set() which calls jpayne@69: get_new_keys_name() and create_new_key_set() to save a custom keyset jpayne@69: and its keybindings to idleConf.userCfg['keys']. jpayne@69: jpayne@69: Listbox bindingslist contains all of the keybindings for the jpayne@69: selected keyset. The keybindings are loaded in load_keys_list() jpayne@69: and are pairs of (event, [keys]) where keys can be a list jpayne@69: of one or more key combinations to bind to the same event. jpayne@69: Mouse button 1 click invokes on_bindingslist_select(), which jpayne@69: allows button_new_keys to be clicked. jpayne@69: jpayne@69: So, an item is selected in listbindings, which activates jpayne@69: button_new_keys, and clicking button_new_keys calls function jpayne@69: get_new_keys(). Function get_new_keys() gets the key mappings from the jpayne@69: current keyset for the binding event item that was selected. The jpayne@69: function then displays another dialog, GetKeysDialog, with the jpayne@69: selected binding event and current keys and allows new key sequences jpayne@69: to be entered for that binding event. If the keys aren't jpayne@69: changed, nothing happens. If the keys are changed and the keyset jpayne@69: is a builtin, function get_new_keys_name() will be called jpayne@69: for input of a custom keyset name. If no name is given, then the jpayne@69: change to the keybinding will abort and no updates will be made. If jpayne@69: a custom name is entered in the prompt or if the current keyset was jpayne@69: already custom (and thus didn't require a prompt), then jpayne@69: idleConf.userCfg['keys'] is updated in function create_new_key_set() jpayne@69: with the change to the event binding. The item listing in bindingslist jpayne@69: is updated with the new keys. Var keybinding is also set which invokes jpayne@69: the callback function, var_changed_keybinding, to add the change to jpayne@69: the 'keys' or 'extensions' changes tracker based on the binding type. jpayne@69: jpayne@69: Tk Variables: jpayne@69: keybinding: Action/key bindings. jpayne@69: jpayne@69: Methods: jpayne@69: load_keys_list: Reload active set. jpayne@69: create_new_key_set: Combine active keyset and changes. jpayne@69: set_keys_type: Command for keyset_source. jpayne@69: save_new_key_set: Save to idleConf.userCfg['keys'] (is function). jpayne@69: deactivate_current_config: Remove keys bindings in editors. jpayne@69: jpayne@69: Widgets for KeysPage(frame): (*) widgets bound to self jpayne@69: frame_key_sets: LabelFrame jpayne@69: frames[0]: Frame jpayne@69: (*)builtin_keyset_on: Radiobutton - var keyset_source jpayne@69: (*)custom_keyset_on: Radiobutton - var keyset_source jpayne@69: (*)builtinlist: DynOptionMenu - var builtin_name, jpayne@69: func keybinding_selected jpayne@69: (*)customlist: DynOptionMenu - var custom_name, jpayne@69: func keybinding_selected jpayne@69: (*)keys_message: Label jpayne@69: frames[1]: Frame jpayne@69: (*)button_delete_custom_keys: Button - delete_custom_keys jpayne@69: (*)button_save_custom_keys: Button - save_as_new_key_set jpayne@69: frame_custom: LabelFrame jpayne@69: frame_target: Frame jpayne@69: target_title: Label jpayne@69: scroll_target_y: Scrollbar jpayne@69: scroll_target_x: Scrollbar jpayne@69: (*)bindingslist: ListBox - on_bindingslist_select jpayne@69: (*)button_new_keys: Button - get_new_keys & ..._name jpayne@69: """ jpayne@69: self.builtin_name = tracers.add( jpayne@69: StringVar(self), self.var_changed_builtin_name) jpayne@69: self.custom_name = tracers.add( jpayne@69: StringVar(self), self.var_changed_custom_name) jpayne@69: self.keyset_source = tracers.add( jpayne@69: BooleanVar(self), self.var_changed_keyset_source) jpayne@69: self.keybinding = tracers.add( jpayne@69: StringVar(self), self.var_changed_keybinding) jpayne@69: jpayne@69: # Create widgets: jpayne@69: # body and section frames. jpayne@69: frame_custom = LabelFrame( jpayne@69: self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Custom Key Bindings ') jpayne@69: frame_key_sets = LabelFrame( jpayne@69: self, borderwidth=2, relief=GROOVE, text=' Key Set ') jpayne@69: # frame_custom. jpayne@69: frame_target = Frame(frame_custom) jpayne@69: target_title = Label(frame_target, text='Action - Key(s)') jpayne@69: scroll_target_y = Scrollbar(frame_target) jpayne@69: scroll_target_x = Scrollbar(frame_target, orient=HORIZONTAL) jpayne@69: self.bindingslist = Listbox( jpayne@69: frame_target, takefocus=FALSE, exportselection=FALSE) jpayne@69: self.bindingslist.bind('', jpayne@69: self.on_bindingslist_select) jpayne@69: scroll_target_y['command'] = self.bindingslist.yview jpayne@69: scroll_target_x['command'] = self.bindingslist.xview jpayne@69: self.bindingslist['yscrollcommand'] = scroll_target_y.set jpayne@69: self.bindingslist['xscrollcommand'] = scroll_target_x.set jpayne@69: self.button_new_keys = Button( jpayne@69: frame_custom, text='Get New Keys for Selection', jpayne@69: command=self.get_new_keys, state='disabled') jpayne@69: # frame_key_sets. jpayne@69: frames = [Frame(frame_key_sets, padding=2, borderwidth=0) jpayne@69: for i in range(2)] jpayne@69: self.builtin_keyset_on = Radiobutton( jpayne@69: frames[0], variable=self.keyset_source, value=1, jpayne@69: command=self.set_keys_type, text='Use a Built-in Key Set') jpayne@69: self.custom_keyset_on = Radiobutton( jpayne@69: frames[0], variable=self.keyset_source, value=0, jpayne@69: command=self.set_keys_type, text='Use a Custom Key Set') jpayne@69: self.builtinlist = DynOptionMenu( jpayne@69: frames[0], self.builtin_name, None, command=None) jpayne@69: self.customlist = DynOptionMenu( jpayne@69: frames[0], self.custom_name, None, command=None) jpayne@69: self.button_delete_custom_keys = Button( jpayne@69: frames[1], text='Delete Custom Key Set', jpayne@69: command=self.delete_custom_keys) jpayne@69: self.button_save_custom_keys = Button( jpayne@69: frames[1], text='Save as New Custom Key Set', jpayne@69: command=self.save_as_new_key_set) jpayne@69: self.keys_message = Label(frames[0], borderwidth=2) jpayne@69: jpayne@69: # Pack widgets: jpayne@69: # body. jpayne@69: frame_custom.pack(side=BOTTOM, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: frame_key_sets.pack(side=BOTTOM, padx=5, pady=5, fill=BOTH) jpayne@69: # frame_custom. jpayne@69: self.button_new_keys.pack(side=BOTTOM, fill=X, padx=5, pady=5) jpayne@69: frame_target.pack(side=LEFT, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: # frame_target. jpayne@69: frame_target.columnconfigure(0, weight=1) jpayne@69: frame_target.rowconfigure(1, weight=1) jpayne@69: target_title.grid(row=0, column=0, columnspan=2, sticky=W) jpayne@69: self.bindingslist.grid(row=1, column=0, sticky=NSEW) jpayne@69: scroll_target_y.grid(row=1, column=1, sticky=NS) jpayne@69: scroll_target_x.grid(row=2, column=0, sticky=EW) jpayne@69: # frame_key_sets. jpayne@69: self.builtin_keyset_on.grid(row=0, column=0, sticky=W+NS) jpayne@69: self.custom_keyset_on.grid(row=1, column=0, sticky=W+NS) jpayne@69: self.builtinlist.grid(row=0, column=1, sticky=NSEW) jpayne@69: self.customlist.grid(row=1, column=1, sticky=NSEW) jpayne@69: self.keys_message.grid(row=0, column=2, sticky=NSEW, padx=5, pady=5) jpayne@69: self.button_delete_custom_keys.pack(side=LEFT, fill=X, expand=True, padx=2) jpayne@69: self.button_save_custom_keys.pack(side=LEFT, fill=X, expand=True, padx=2) jpayne@69: frames[0].pack(side=TOP, fill=BOTH, expand=True) jpayne@69: frames[1].pack(side=TOP, fill=X, expand=True, pady=2) jpayne@69: jpayne@69: def load_key_cfg(self): jpayne@69: "Load current configuration settings for the keybinding options." jpayne@69: # Set current keys type radiobutton. jpayne@69: self.keyset_source.set(idleConf.GetOption( jpayne@69: 'main', 'Keys', 'default', type='bool', default=1)) jpayne@69: # Set current keys. jpayne@69: current_option = idleConf.CurrentKeys() jpayne@69: # Load available keyset option menus. jpayne@69: if self.keyset_source.get(): # Default theme selected. jpayne@69: item_list = idleConf.GetSectionList('default', 'keys') jpayne@69: item_list.sort() jpayne@69: self.builtinlist.SetMenu(item_list, current_option) jpayne@69: item_list = idleConf.GetSectionList('user', 'keys') jpayne@69: item_list.sort() jpayne@69: if not item_list: jpayne@69: self.custom_keyset_on.state(('disabled',)) jpayne@69: self.custom_name.set('- no custom keys -') jpayne@69: else: jpayne@69: self.customlist.SetMenu(item_list, item_list[0]) jpayne@69: else: # User key set selected. jpayne@69: item_list = idleConf.GetSectionList('user', 'keys') jpayne@69: item_list.sort() jpayne@69: self.customlist.SetMenu(item_list, current_option) jpayne@69: item_list = idleConf.GetSectionList('default', 'keys') jpayne@69: item_list.sort() jpayne@69: self.builtinlist.SetMenu(item_list, idleConf.default_keys()) jpayne@69: self.set_keys_type() jpayne@69: # Load keyset element list. jpayne@69: keyset_name = idleConf.CurrentKeys() jpayne@69: self.load_keys_list(keyset_name) jpayne@69: jpayne@69: def var_changed_builtin_name(self, *params): jpayne@69: "Process selection of builtin key set." jpayne@69: old_keys = ( jpayne@69: 'IDLE Classic Windows', jpayne@69: 'IDLE Classic Unix', jpayne@69: 'IDLE Classic Mac', jpayne@69: 'IDLE Classic OSX', jpayne@69: ) jpayne@69: value = self.builtin_name.get() jpayne@69: if value not in old_keys: jpayne@69: if idleConf.GetOption('main', 'Keys', 'name') not in old_keys: jpayne@69: changes.add_option('main', 'Keys', 'name', old_keys[0]) jpayne@69: changes.add_option('main', 'Keys', 'name2', value) jpayne@69: self.keys_message['text'] = 'New key set, see Help' jpayne@69: else: jpayne@69: changes.add_option('main', 'Keys', 'name', value) jpayne@69: changes.add_option('main', 'Keys', 'name2', '') jpayne@69: self.keys_message['text'] = '' jpayne@69: self.load_keys_list(value) jpayne@69: jpayne@69: def var_changed_custom_name(self, *params): jpayne@69: "Process selection of custom key set." jpayne@69: value = self.custom_name.get() jpayne@69: if value != '- no custom keys -': jpayne@69: changes.add_option('main', 'Keys', 'name', value) jpayne@69: self.load_keys_list(value) jpayne@69: jpayne@69: def var_changed_keyset_source(self, *params): jpayne@69: "Process toggle between builtin key set and custom key set." jpayne@69: value = self.keyset_source.get() jpayne@69: changes.add_option('main', 'Keys', 'default', value) jpayne@69: if value: jpayne@69: self.var_changed_builtin_name() jpayne@69: else: jpayne@69: self.var_changed_custom_name() jpayne@69: jpayne@69: def var_changed_keybinding(self, *params): jpayne@69: "Store change to a keybinding." jpayne@69: value = self.keybinding.get() jpayne@69: key_set = self.custom_name.get() jpayne@69: event = self.bindingslist.get(ANCHOR).split()[0] jpayne@69: if idleConf.IsCoreBinding(event): jpayne@69: changes.add_option('keys', key_set, event, value) jpayne@69: else: # Event is an extension binding. jpayne@69: ext_name = idleConf.GetExtnNameForEvent(event) jpayne@69: ext_keybind_section = ext_name + '_cfgBindings' jpayne@69: changes.add_option('extensions', ext_keybind_section, event, value) jpayne@69: jpayne@69: def set_keys_type(self): jpayne@69: "Set available screen options based on builtin or custom key set." jpayne@69: if self.keyset_source.get(): jpayne@69: self.builtinlist['state'] = 'normal' jpayne@69: self.customlist['state'] = 'disabled' jpayne@69: self.button_delete_custom_keys.state(('disabled',)) jpayne@69: else: jpayne@69: self.builtinlist['state'] = 'disabled' jpayne@69: self.custom_keyset_on.state(('!disabled',)) jpayne@69: self.customlist['state'] = 'normal' jpayne@69: self.button_delete_custom_keys.state(('!disabled',)) jpayne@69: jpayne@69: def get_new_keys(self): jpayne@69: """Handle event to change key binding for selected line. jpayne@69: jpayne@69: A selection of a key/binding in the list of current jpayne@69: bindings pops up a dialog to enter a new binding. If jpayne@69: the current key set is builtin and a binding has jpayne@69: changed, then a name for a custom key set needs to be jpayne@69: entered for the change to be applied. jpayne@69: """ jpayne@69: list_index = self.bindingslist.index(ANCHOR) jpayne@69: binding = self.bindingslist.get(list_index) jpayne@69: bind_name = binding.split()[0] jpayne@69: if self.keyset_source.get(): jpayne@69: current_key_set_name = self.builtin_name.get() jpayne@69: else: jpayne@69: current_key_set_name = self.custom_name.get() jpayne@69: current_bindings = idleConf.GetCurrentKeySet() jpayne@69: if current_key_set_name in changes['keys']: # unsaved changes jpayne@69: key_set_changes = changes['keys'][current_key_set_name] jpayne@69: for event in key_set_changes: jpayne@69: current_bindings[event] = key_set_changes[event].split() jpayne@69: current_key_sequences = list(current_bindings.values()) jpayne@69: new_keys = GetKeysDialog(self, 'Get New Keys', bind_name, jpayne@69: current_key_sequences).result jpayne@69: if new_keys: jpayne@69: if self.keyset_source.get(): # Current key set is a built-in. jpayne@69: message = ('Your changes will be saved as a new Custom Key Set.' jpayne@69: ' Enter a name for your new Custom Key Set below.') jpayne@69: new_keyset = self.get_new_keys_name(message) jpayne@69: if not new_keyset: # User cancelled custom key set creation. jpayne@69: self.bindingslist.select_set(list_index) jpayne@69: self.bindingslist.select_anchor(list_index) jpayne@69: return jpayne@69: else: # Create new custom key set based on previously active key set. jpayne@69: self.create_new_key_set(new_keyset) jpayne@69: self.bindingslist.delete(list_index) jpayne@69: self.bindingslist.insert(list_index, bind_name+' - '+new_keys) jpayne@69: self.bindingslist.select_set(list_index) jpayne@69: self.bindingslist.select_anchor(list_index) jpayne@69: self.keybinding.set(new_keys) jpayne@69: else: jpayne@69: self.bindingslist.select_set(list_index) jpayne@69: self.bindingslist.select_anchor(list_index) jpayne@69: jpayne@69: def get_new_keys_name(self, message): jpayne@69: "Return new key set name from query popup." jpayne@69: used_names = (idleConf.GetSectionList('user', 'keys') + jpayne@69: idleConf.GetSectionList('default', 'keys')) jpayne@69: new_keyset = SectionName( jpayne@69: self, 'New Custom Key Set', message, used_names).result jpayne@69: return new_keyset jpayne@69: jpayne@69: def save_as_new_key_set(self): jpayne@69: "Prompt for name of new key set and save changes using that name." jpayne@69: new_keys_name = self.get_new_keys_name('New Key Set Name:') jpayne@69: if new_keys_name: jpayne@69: self.create_new_key_set(new_keys_name) jpayne@69: jpayne@69: def on_bindingslist_select(self, event): jpayne@69: "Activate button to assign new keys to selected action." jpayne@69: self.button_new_keys.state(('!disabled',)) jpayne@69: jpayne@69: def create_new_key_set(self, new_key_set_name): jpayne@69: """Create a new custom key set with the given name. jpayne@69: jpayne@69: Copy the bindings/keys from the previously active keyset jpayne@69: to the new keyset and activate the new custom keyset. jpayne@69: """ jpayne@69: if self.keyset_source.get(): jpayne@69: prev_key_set_name = self.builtin_name.get() jpayne@69: else: jpayne@69: prev_key_set_name = self.custom_name.get() jpayne@69: prev_keys = idleConf.GetCoreKeys(prev_key_set_name) jpayne@69: new_keys = {} jpayne@69: for event in prev_keys: # Add key set to changed items. jpayne@69: event_name = event[2:-2] # Trim off the angle brackets. jpayne@69: binding = ' '.join(prev_keys[event]) jpayne@69: new_keys[event_name] = binding jpayne@69: # Handle any unsaved changes to prev key set. jpayne@69: if prev_key_set_name in changes['keys']: jpayne@69: key_set_changes = changes['keys'][prev_key_set_name] jpayne@69: for event in key_set_changes: jpayne@69: new_keys[event] = key_set_changes[event] jpayne@69: # Save the new key set. jpayne@69: self.save_new_key_set(new_key_set_name, new_keys) jpayne@69: # Change GUI over to the new key set. jpayne@69: custom_key_list = idleConf.GetSectionList('user', 'keys') jpayne@69: custom_key_list.sort() jpayne@69: self.customlist.SetMenu(custom_key_list, new_key_set_name) jpayne@69: self.keyset_source.set(0) jpayne@69: self.set_keys_type() jpayne@69: jpayne@69: def load_keys_list(self, keyset_name): jpayne@69: """Reload the list of action/key binding pairs for the active key set. jpayne@69: jpayne@69: An action/key binding can be selected to change the key binding. jpayne@69: """ jpayne@69: reselect = False jpayne@69: if self.bindingslist.curselection(): jpayne@69: reselect = True jpayne@69: list_index = self.bindingslist.index(ANCHOR) jpayne@69: keyset = idleConf.GetKeySet(keyset_name) jpayne@69: bind_names = list(keyset.keys()) jpayne@69: bind_names.sort() jpayne@69: self.bindingslist.delete(0, END) jpayne@69: for bind_name in bind_names: jpayne@69: key = ' '.join(keyset[bind_name]) jpayne@69: bind_name = bind_name[2:-2] # Trim off the angle brackets. jpayne@69: if keyset_name in changes['keys']: jpayne@69: # Handle any unsaved changes to this key set. jpayne@69: if bind_name in changes['keys'][keyset_name]: jpayne@69: key = changes['keys'][keyset_name][bind_name] jpayne@69: self.bindingslist.insert(END, bind_name+' - '+key) jpayne@69: if reselect: jpayne@69: self.bindingslist.see(list_index) jpayne@69: self.bindingslist.select_set(list_index) jpayne@69: self.bindingslist.select_anchor(list_index) jpayne@69: jpayne@69: @staticmethod jpayne@69: def save_new_key_set(keyset_name, keyset): jpayne@69: """Save a newly created core key set. jpayne@69: jpayne@69: Add keyset to idleConf.userCfg['keys'], not to disk. jpayne@69: If the keyset doesn't exist, it is created. The jpayne@69: binding/keys are taken from the keyset argument. jpayne@69: jpayne@69: keyset_name - string, the name of the new key set jpayne@69: keyset - dictionary containing the new keybindings jpayne@69: """ jpayne@69: if not idleConf.userCfg['keys'].has_section(keyset_name): jpayne@69: idleConf.userCfg['keys'].add_section(keyset_name) jpayne@69: for event in keyset: jpayne@69: value = keyset[event] jpayne@69: idleConf.userCfg['keys'].SetOption(keyset_name, event, value) jpayne@69: jpayne@69: def askyesno(self, *args, **kwargs): jpayne@69: # Make testing easier. Could change implementation. jpayne@69: return messagebox.askyesno(*args, **kwargs) jpayne@69: jpayne@69: def delete_custom_keys(self): jpayne@69: """Handle event to delete a custom key set. jpayne@69: jpayne@69: Applying the delete deactivates the current configuration and jpayne@69: reverts to the default. The custom key set is permanently jpayne@69: deleted from the config file. jpayne@69: """ jpayne@69: keyset_name = self.custom_name.get() jpayne@69: delmsg = 'Are you sure you wish to delete the key set %r ?' jpayne@69: if not self.askyesno( jpayne@69: 'Delete Key Set', delmsg % keyset_name, parent=self): jpayne@69: return jpayne@69: self.cd.deactivate_current_config() jpayne@69: # Remove key set from changes, config, and file. jpayne@69: changes.delete_section('keys', keyset_name) jpayne@69: # Reload user key set list. jpayne@69: item_list = idleConf.GetSectionList('user', 'keys') jpayne@69: item_list.sort() jpayne@69: if not item_list: jpayne@69: self.custom_keyset_on.state(('disabled',)) jpayne@69: self.customlist.SetMenu(item_list, '- no custom keys -') jpayne@69: else: jpayne@69: self.customlist.SetMenu(item_list, item_list[0]) jpayne@69: # Revert to default key set. jpayne@69: self.keyset_source.set(idleConf.defaultCfg['main'] jpayne@69: .Get('Keys', 'default')) jpayne@69: self.builtin_name.set(idleConf.defaultCfg['main'].Get('Keys', 'name') jpayne@69: or idleConf.default_keys()) jpayne@69: # User can't back out of these changes, they must be applied now. jpayne@69: changes.save_all() jpayne@69: self.cd.save_all_changed_extensions() jpayne@69: self.cd.activate_config_changes() jpayne@69: self.set_keys_type() jpayne@69: jpayne@69: jpayne@69: class GenPage(Frame): jpayne@69: jpayne@69: def __init__(self, master): jpayne@69: super().__init__(master) jpayne@69: jpayne@69: self.init_validators() jpayne@69: self.create_page_general() jpayne@69: self.load_general_cfg() jpayne@69: jpayne@69: def init_validators(self): jpayne@69: digits_or_empty_re = re.compile(r'[0-9]*') jpayne@69: def is_digits_or_empty(s): jpayne@69: "Return 's is blank or contains only digits'" jpayne@69: return digits_or_empty_re.fullmatch(s) is not None jpayne@69: self.digits_only = (self.register(is_digits_or_empty), '%P',) jpayne@69: jpayne@69: def create_page_general(self): jpayne@69: """Return frame of widgets for General tab. jpayne@69: jpayne@69: Enable users to provisionally change general options. Function jpayne@69: load_general_cfg initializes tk variables and helplist using jpayne@69: idleConf. Radiobuttons startup_shell_on and startup_editor_on jpayne@69: set var startup_edit. Radiobuttons save_ask_on and save_auto_on jpayne@69: set var autosave. Entry boxes win_width_int and win_height_int jpayne@69: set var win_width and win_height. Setting var_name invokes the jpayne@69: default callback that adds option to changes. jpayne@69: jpayne@69: Helplist: load_general_cfg loads list user_helplist with jpayne@69: name, position pairs and copies names to listbox helplist. jpayne@69: Clicking a name invokes help_source selected. Clicking jpayne@69: button_helplist_name invokes helplist_item_name, which also jpayne@69: changes user_helplist. These functions all call jpayne@69: set_add_delete_state. All but load call update_help_changes to jpayne@69: rewrite changes['main']['HelpFiles']. jpayne@69: jpayne@69: Widgets for GenPage(Frame): (*) widgets bound to self jpayne@69: frame_window: LabelFrame jpayne@69: frame_run: Frame jpayne@69: startup_title: Label jpayne@69: (*)startup_editor_on: Radiobutton - startup_edit jpayne@69: (*)startup_shell_on: Radiobutton - startup_edit jpayne@69: frame_win_size: Frame jpayne@69: win_size_title: Label jpayne@69: win_width_title: Label jpayne@69: (*)win_width_int: Entry - win_width jpayne@69: win_height_title: Label jpayne@69: (*)win_height_int: Entry - win_height jpayne@69: frame_cursor_blink: Frame jpayne@69: cursor_blink_title: Label jpayne@69: (*)cursor_blink_bool: Checkbutton - cursor_blink jpayne@69: frame_autocomplete: Frame jpayne@69: auto_wait_title: Label jpayne@69: (*)auto_wait_int: Entry - autocomplete_wait jpayne@69: frame_paren1: Frame jpayne@69: paren_style_title: Label jpayne@69: (*)paren_style_type: OptionMenu - paren_style jpayne@69: frame_paren2: Frame jpayne@69: paren_time_title: Label jpayne@69: (*)paren_flash_time: Entry - flash_delay jpayne@69: (*)bell_on: Checkbutton - paren_bell jpayne@69: frame_editor: LabelFrame jpayne@69: frame_save: Frame jpayne@69: run_save_title: Label jpayne@69: (*)save_ask_on: Radiobutton - autosave jpayne@69: (*)save_auto_on: Radiobutton - autosave jpayne@69: frame_format: Frame jpayne@69: format_width_title: Label jpayne@69: (*)format_width_int: Entry - format_width jpayne@69: frame_line_numbers_default: Frame jpayne@69: line_numbers_default_title: Label jpayne@69: (*)line_numbers_default_bool: Checkbutton - line_numbers_default jpayne@69: frame_context: Frame jpayne@69: context_title: Label jpayne@69: (*)context_int: Entry - context_lines jpayne@69: frame_shell: LabelFrame jpayne@69: frame_auto_squeeze_min_lines: Frame jpayne@69: auto_squeeze_min_lines_title: Label jpayne@69: (*)auto_squeeze_min_lines_int: Entry - auto_squeeze_min_lines jpayne@69: frame_help: LabelFrame jpayne@69: frame_helplist: Frame jpayne@69: frame_helplist_buttons: Frame jpayne@69: (*)button_helplist_edit jpayne@69: (*)button_helplist_add jpayne@69: (*)button_helplist_remove jpayne@69: (*)helplist: ListBox jpayne@69: scroll_helplist: Scrollbar jpayne@69: """ jpayne@69: # Integer values need StringVar because int('') raises. jpayne@69: self.startup_edit = tracers.add( jpayne@69: IntVar(self), ('main', 'General', 'editor-on-startup')) jpayne@69: self.win_width = tracers.add( jpayne@69: StringVar(self), ('main', 'EditorWindow', 'width')) jpayne@69: self.win_height = tracers.add( jpayne@69: StringVar(self), ('main', 'EditorWindow', 'height')) jpayne@69: self.cursor_blink = tracers.add( jpayne@69: BooleanVar(self), ('main', 'EditorWindow', 'cursor-blink')) jpayne@69: self.autocomplete_wait = tracers.add( jpayne@69: StringVar(self), ('extensions', 'AutoComplete', 'popupwait')) jpayne@69: self.paren_style = tracers.add( jpayne@69: StringVar(self), ('extensions', 'ParenMatch', 'style')) jpayne@69: self.flash_delay = tracers.add( jpayne@69: StringVar(self), ('extensions', 'ParenMatch', 'flash-delay')) jpayne@69: self.paren_bell = tracers.add( jpayne@69: BooleanVar(self), ('extensions', 'ParenMatch', 'bell')) jpayne@69: jpayne@69: self.auto_squeeze_min_lines = tracers.add( jpayne@69: StringVar(self), ('main', 'PyShell', 'auto-squeeze-min-lines')) jpayne@69: jpayne@69: self.autosave = tracers.add( jpayne@69: IntVar(self), ('main', 'General', 'autosave')) jpayne@69: self.format_width = tracers.add( jpayne@69: StringVar(self), ('extensions', 'FormatParagraph', 'max-width')) jpayne@69: self.line_numbers_default = tracers.add( jpayne@69: BooleanVar(self), jpayne@69: ('main', 'EditorWindow', 'line-numbers-default')) jpayne@69: self.context_lines = tracers.add( jpayne@69: StringVar(self), ('extensions', 'CodeContext', 'maxlines')) jpayne@69: jpayne@69: # Create widgets: jpayne@69: # Section frames. jpayne@69: frame_window = LabelFrame(self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Window Preferences') jpayne@69: frame_editor = LabelFrame(self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Editor Preferences') jpayne@69: frame_shell = LabelFrame(self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Shell Preferences') jpayne@69: frame_help = LabelFrame(self, borderwidth=2, relief=GROOVE, jpayne@69: text=' Additional Help Sources ') jpayne@69: # Frame_window. jpayne@69: frame_run = Frame(frame_window, borderwidth=0) jpayne@69: startup_title = Label(frame_run, text='At Startup') jpayne@69: self.startup_editor_on = Radiobutton( jpayne@69: frame_run, variable=self.startup_edit, value=1, jpayne@69: text="Open Edit Window") jpayne@69: self.startup_shell_on = Radiobutton( jpayne@69: frame_run, variable=self.startup_edit, value=0, jpayne@69: text='Open Shell Window') jpayne@69: jpayne@69: frame_win_size = Frame(frame_window, borderwidth=0) jpayne@69: win_size_title = Label( jpayne@69: frame_win_size, text='Initial Window Size (in characters)') jpayne@69: win_width_title = Label(frame_win_size, text='Width') jpayne@69: self.win_width_int = Entry( jpayne@69: frame_win_size, textvariable=self.win_width, width=3, jpayne@69: validatecommand=self.digits_only, validate='key', jpayne@69: ) jpayne@69: win_height_title = Label(frame_win_size, text='Height') jpayne@69: self.win_height_int = Entry( jpayne@69: frame_win_size, textvariable=self.win_height, width=3, jpayne@69: validatecommand=self.digits_only, validate='key', jpayne@69: ) jpayne@69: jpayne@69: frame_cursor_blink = Frame(frame_window, borderwidth=0) jpayne@69: cursor_blink_title = Label(frame_cursor_blink, text='Cursor Blink') jpayne@69: self.cursor_blink_bool = Checkbutton(frame_cursor_blink, jpayne@69: variable=self.cursor_blink, width=1) jpayne@69: jpayne@69: frame_autocomplete = Frame(frame_window, borderwidth=0,) jpayne@69: auto_wait_title = Label(frame_autocomplete, jpayne@69: text='Completions Popup Wait (milliseconds)') jpayne@69: self.auto_wait_int = Entry(frame_autocomplete, width=6, jpayne@69: textvariable=self.autocomplete_wait, jpayne@69: validatecommand=self.digits_only, jpayne@69: validate='key', jpayne@69: ) jpayne@69: jpayne@69: frame_paren1 = Frame(frame_window, borderwidth=0) jpayne@69: paren_style_title = Label(frame_paren1, text='Paren Match Style') jpayne@69: self.paren_style_type = OptionMenu( jpayne@69: frame_paren1, self.paren_style, 'expression', jpayne@69: "opener","parens","expression") jpayne@69: frame_paren2 = Frame(frame_window, borderwidth=0) jpayne@69: paren_time_title = Label( jpayne@69: frame_paren2, text='Time Match Displayed (milliseconds)\n' jpayne@69: '(0 is until next input)') jpayne@69: self.paren_flash_time = Entry( jpayne@69: frame_paren2, textvariable=self.flash_delay, width=6) jpayne@69: self.bell_on = Checkbutton( jpayne@69: frame_paren2, text="Bell on Mismatch", variable=self.paren_bell) jpayne@69: jpayne@69: # Frame_editor. jpayne@69: frame_save = Frame(frame_editor, borderwidth=0) jpayne@69: run_save_title = Label(frame_save, text='At Start of Run (F5) ') jpayne@69: self.save_ask_on = Radiobutton( jpayne@69: frame_save, variable=self.autosave, value=0, jpayne@69: text="Prompt to Save") jpayne@69: self.save_auto_on = Radiobutton( jpayne@69: frame_save, variable=self.autosave, value=1, jpayne@69: text='No Prompt') jpayne@69: jpayne@69: frame_format = Frame(frame_editor, borderwidth=0) jpayne@69: format_width_title = Label(frame_format, jpayne@69: text='Format Paragraph Max Width') jpayne@69: self.format_width_int = Entry( jpayne@69: frame_format, textvariable=self.format_width, width=4, jpayne@69: validatecommand=self.digits_only, validate='key', jpayne@69: ) jpayne@69: jpayne@69: frame_line_numbers_default = Frame(frame_editor, borderwidth=0) jpayne@69: line_numbers_default_title = Label( jpayne@69: frame_line_numbers_default, text='Show line numbers in new windows') jpayne@69: self.line_numbers_default_bool = Checkbutton( jpayne@69: frame_line_numbers_default, jpayne@69: variable=self.line_numbers_default, jpayne@69: width=1) jpayne@69: jpayne@69: frame_context = Frame(frame_editor, borderwidth=0) jpayne@69: context_title = Label(frame_context, text='Max Context Lines :') jpayne@69: self.context_int = Entry( jpayne@69: frame_context, textvariable=self.context_lines, width=3, jpayne@69: validatecommand=self.digits_only, validate='key', jpayne@69: ) jpayne@69: jpayne@69: # Frame_shell. jpayne@69: frame_auto_squeeze_min_lines = Frame(frame_shell, borderwidth=0) jpayne@69: auto_squeeze_min_lines_title = Label(frame_auto_squeeze_min_lines, jpayne@69: text='Auto-Squeeze Min. Lines:') jpayne@69: self.auto_squeeze_min_lines_int = Entry( jpayne@69: frame_auto_squeeze_min_lines, width=4, jpayne@69: textvariable=self.auto_squeeze_min_lines, jpayne@69: validatecommand=self.digits_only, validate='key', jpayne@69: ) jpayne@69: jpayne@69: # frame_help. jpayne@69: frame_helplist = Frame(frame_help) jpayne@69: frame_helplist_buttons = Frame(frame_helplist) jpayne@69: self.helplist = Listbox( jpayne@69: frame_helplist, height=5, takefocus=True, jpayne@69: exportselection=FALSE) jpayne@69: scroll_helplist = Scrollbar(frame_helplist) jpayne@69: scroll_helplist['command'] = self.helplist.yview jpayne@69: self.helplist['yscrollcommand'] = scroll_helplist.set jpayne@69: self.helplist.bind('', self.help_source_selected) jpayne@69: self.button_helplist_edit = Button( jpayne@69: frame_helplist_buttons, text='Edit', state='disabled', jpayne@69: width=8, command=self.helplist_item_edit) jpayne@69: self.button_helplist_add = Button( jpayne@69: frame_helplist_buttons, text='Add', jpayne@69: width=8, command=self.helplist_item_add) jpayne@69: self.button_helplist_remove = Button( jpayne@69: frame_helplist_buttons, text='Remove', state='disabled', jpayne@69: width=8, command=self.helplist_item_remove) jpayne@69: jpayne@69: # Pack widgets: jpayne@69: # Body. jpayne@69: frame_window.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: frame_editor.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: frame_shell.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: frame_help.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: # frame_run. jpayne@69: frame_run.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: startup_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.startup_shell_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) jpayne@69: self.startup_editor_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) jpayne@69: # frame_win_size. jpayne@69: frame_win_size.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: win_size_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.win_height_int.pack(side=RIGHT, anchor=E, padx=10, pady=5) jpayne@69: win_height_title.pack(side=RIGHT, anchor=E, pady=5) jpayne@69: self.win_width_int.pack(side=RIGHT, anchor=E, padx=10, pady=5) jpayne@69: win_width_title.pack(side=RIGHT, anchor=E, pady=5) jpayne@69: # frame_cursor_blink. jpayne@69: frame_cursor_blink.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: cursor_blink_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.cursor_blink_bool.pack(side=LEFT, padx=5, pady=5) jpayne@69: # frame_autocomplete. jpayne@69: frame_autocomplete.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: auto_wait_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.auto_wait_int.pack(side=TOP, padx=10, pady=5) jpayne@69: # frame_paren. jpayne@69: frame_paren1.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: paren_style_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.paren_style_type.pack(side=TOP, padx=10, pady=5) jpayne@69: frame_paren2.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: paren_time_title.pack(side=LEFT, anchor=W, padx=5) jpayne@69: self.bell_on.pack(side=RIGHT, anchor=E, padx=15, pady=5) jpayne@69: self.paren_flash_time.pack(side=TOP, anchor=W, padx=15, pady=5) jpayne@69: jpayne@69: # frame_save. jpayne@69: frame_save.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: run_save_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.save_auto_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) jpayne@69: self.save_ask_on.pack(side=RIGHT, anchor=W, padx=5, pady=5) jpayne@69: # frame_format. jpayne@69: frame_format.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: format_width_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.format_width_int.pack(side=TOP, padx=10, pady=5) jpayne@69: # frame_line_numbers_default. jpayne@69: frame_line_numbers_default.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: line_numbers_default_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.line_numbers_default_bool.pack(side=LEFT, padx=5, pady=5) jpayne@69: # frame_context. jpayne@69: frame_context.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: context_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.context_int.pack(side=TOP, padx=5, pady=5) jpayne@69: jpayne@69: # frame_auto_squeeze_min_lines jpayne@69: frame_auto_squeeze_min_lines.pack(side=TOP, padx=5, pady=0, fill=X) jpayne@69: auto_squeeze_min_lines_title.pack(side=LEFT, anchor=W, padx=5, pady=5) jpayne@69: self.auto_squeeze_min_lines_int.pack(side=TOP, padx=5, pady=5) jpayne@69: jpayne@69: # frame_help. jpayne@69: frame_helplist_buttons.pack(side=RIGHT, padx=5, pady=5, fill=Y) jpayne@69: frame_helplist.pack(side=TOP, padx=5, pady=5, expand=TRUE, fill=BOTH) jpayne@69: scroll_helplist.pack(side=RIGHT, anchor=W, fill=Y) jpayne@69: self.helplist.pack(side=LEFT, anchor=E, expand=TRUE, fill=BOTH) jpayne@69: self.button_helplist_edit.pack(side=TOP, anchor=W, pady=5) jpayne@69: self.button_helplist_add.pack(side=TOP, anchor=W) jpayne@69: self.button_helplist_remove.pack(side=TOP, anchor=W, pady=5) jpayne@69: jpayne@69: def load_general_cfg(self): jpayne@69: "Load current configuration settings for the general options." jpayne@69: # Set variables for all windows. jpayne@69: self.startup_edit.set(idleConf.GetOption( jpayne@69: 'main', 'General', 'editor-on-startup', type='bool')) jpayne@69: self.win_width.set(idleConf.GetOption( jpayne@69: 'main', 'EditorWindow', 'width', type='int')) jpayne@69: self.win_height.set(idleConf.GetOption( jpayne@69: 'main', 'EditorWindow', 'height', type='int')) jpayne@69: self.cursor_blink.set(idleConf.GetOption( jpayne@69: 'main', 'EditorWindow', 'cursor-blink', type='bool')) jpayne@69: self.autocomplete_wait.set(idleConf.GetOption( jpayne@69: 'extensions', 'AutoComplete', 'popupwait', type='int')) jpayne@69: self.paren_style.set(idleConf.GetOption( jpayne@69: 'extensions', 'ParenMatch', 'style')) jpayne@69: self.flash_delay.set(idleConf.GetOption( jpayne@69: 'extensions', 'ParenMatch', 'flash-delay', type='int')) jpayne@69: self.paren_bell.set(idleConf.GetOption( jpayne@69: 'extensions', 'ParenMatch', 'bell')) jpayne@69: jpayne@69: # Set variables for editor windows. jpayne@69: self.autosave.set(idleConf.GetOption( jpayne@69: 'main', 'General', 'autosave', default=0, type='bool')) jpayne@69: self.format_width.set(idleConf.GetOption( jpayne@69: 'extensions', 'FormatParagraph', 'max-width', type='int')) jpayne@69: self.line_numbers_default.set(idleConf.GetOption( jpayne@69: 'main', 'EditorWindow', 'line-numbers-default', type='bool')) jpayne@69: self.context_lines.set(idleConf.GetOption( jpayne@69: 'extensions', 'CodeContext', 'maxlines', type='int')) jpayne@69: jpayne@69: # Set variables for shell windows. jpayne@69: self.auto_squeeze_min_lines.set(idleConf.GetOption( jpayne@69: 'main', 'PyShell', 'auto-squeeze-min-lines', type='int')) jpayne@69: jpayne@69: # Set additional help sources. jpayne@69: self.user_helplist = idleConf.GetAllExtraHelpSourcesList() jpayne@69: self.helplist.delete(0, 'end') jpayne@69: for help_item in self.user_helplist: jpayne@69: self.helplist.insert(END, help_item[0]) jpayne@69: self.set_add_delete_state() jpayne@69: jpayne@69: def help_source_selected(self, event): jpayne@69: "Handle event for selecting additional help." jpayne@69: self.set_add_delete_state() jpayne@69: jpayne@69: def set_add_delete_state(self): jpayne@69: "Toggle the state for the help list buttons based on list entries." jpayne@69: if self.helplist.size() < 1: # No entries in list. jpayne@69: self.button_helplist_edit.state(('disabled',)) jpayne@69: self.button_helplist_remove.state(('disabled',)) jpayne@69: else: # Some entries. jpayne@69: if self.helplist.curselection(): # There currently is a selection. jpayne@69: self.button_helplist_edit.state(('!disabled',)) jpayne@69: self.button_helplist_remove.state(('!disabled',)) jpayne@69: else: # There currently is not a selection. jpayne@69: self.button_helplist_edit.state(('disabled',)) jpayne@69: self.button_helplist_remove.state(('disabled',)) jpayne@69: jpayne@69: def helplist_item_add(self): jpayne@69: """Handle add button for the help list. jpayne@69: jpayne@69: Query for name and location of new help sources and add jpayne@69: them to the list. jpayne@69: """ jpayne@69: help_source = HelpSource(self, 'New Help Source').result jpayne@69: if help_source: jpayne@69: self.user_helplist.append(help_source) jpayne@69: self.helplist.insert(END, help_source[0]) jpayne@69: self.update_help_changes() jpayne@69: jpayne@69: def helplist_item_edit(self): jpayne@69: """Handle edit button for the help list. jpayne@69: jpayne@69: Query with existing help source information and update jpayne@69: config if the values are changed. jpayne@69: """ jpayne@69: item_index = self.helplist.index(ANCHOR) jpayne@69: help_source = self.user_helplist[item_index] jpayne@69: new_help_source = HelpSource( jpayne@69: self, 'Edit Help Source', jpayne@69: menuitem=help_source[0], jpayne@69: filepath=help_source[1], jpayne@69: ).result jpayne@69: if new_help_source and new_help_source != help_source: jpayne@69: self.user_helplist[item_index] = new_help_source jpayne@69: self.helplist.delete(item_index) jpayne@69: self.helplist.insert(item_index, new_help_source[0]) jpayne@69: self.update_help_changes() jpayne@69: self.set_add_delete_state() # Selected will be un-selected jpayne@69: jpayne@69: def helplist_item_remove(self): jpayne@69: """Handle remove button for the help list. jpayne@69: jpayne@69: Delete the help list item from config. jpayne@69: """ jpayne@69: item_index = self.helplist.index(ANCHOR) jpayne@69: del(self.user_helplist[item_index]) jpayne@69: self.helplist.delete(item_index) jpayne@69: self.update_help_changes() jpayne@69: self.set_add_delete_state() jpayne@69: jpayne@69: def update_help_changes(self): jpayne@69: "Clear and rebuild the HelpFiles section in changes" jpayne@69: changes['main']['HelpFiles'] = {} jpayne@69: for num in range(1, len(self.user_helplist) + 1): jpayne@69: changes.add_option( jpayne@69: 'main', 'HelpFiles', str(num), jpayne@69: ';'.join(self.user_helplist[num-1][:2])) jpayne@69: jpayne@69: jpayne@69: class VarTrace: jpayne@69: """Maintain Tk variables trace state.""" jpayne@69: jpayne@69: def __init__(self): jpayne@69: """Store Tk variables and callbacks. jpayne@69: jpayne@69: untraced: List of tuples (var, callback) jpayne@69: that do not have the callback attached jpayne@69: to the Tk var. jpayne@69: traced: List of tuples (var, callback) where jpayne@69: that callback has been attached to the var. jpayne@69: """ jpayne@69: self.untraced = [] jpayne@69: self.traced = [] jpayne@69: jpayne@69: def clear(self): jpayne@69: "Clear lists (for tests)." jpayne@69: # Call after all tests in a module to avoid memory leaks. jpayne@69: self.untraced.clear() jpayne@69: self.traced.clear() jpayne@69: jpayne@69: def add(self, var, callback): jpayne@69: """Add (var, callback) tuple to untraced list. jpayne@69: jpayne@69: Args: jpayne@69: var: Tk variable instance. jpayne@69: callback: Either function name to be used as a callback jpayne@69: or a tuple with IdleConf config-type, section, and jpayne@69: option names used in the default callback. jpayne@69: jpayne@69: Return: jpayne@69: Tk variable instance. jpayne@69: """ jpayne@69: if isinstance(callback, tuple): jpayne@69: callback = self.make_callback(var, callback) jpayne@69: self.untraced.append((var, callback)) jpayne@69: return var jpayne@69: jpayne@69: @staticmethod jpayne@69: def make_callback(var, config): jpayne@69: "Return default callback function to add values to changes instance." jpayne@69: def default_callback(*params): jpayne@69: "Add config values to changes instance." jpayne@69: changes.add_option(*config, var.get()) jpayne@69: return default_callback jpayne@69: jpayne@69: def attach(self): jpayne@69: "Attach callback to all vars that are not traced." jpayne@69: while self.untraced: jpayne@69: var, callback = self.untraced.pop() jpayne@69: var.trace_add('write', callback) jpayne@69: self.traced.append((var, callback)) jpayne@69: jpayne@69: def detach(self): jpayne@69: "Remove callback from traced vars." jpayne@69: while self.traced: jpayne@69: var, callback = self.traced.pop() jpayne@69: var.trace_remove('write', var.trace_info()[0][1]) jpayne@69: self.untraced.append((var, callback)) jpayne@69: jpayne@69: jpayne@69: tracers = VarTrace() jpayne@69: jpayne@69: help_common = '''\ jpayne@69: When you click either the Apply or Ok buttons, settings in this jpayne@69: dialog that are different from IDLE's default are saved in jpayne@69: a .idlerc directory in your home directory. Except as noted, jpayne@69: these changes apply to all versions of IDLE installed on this jpayne@69: machine. [Cancel] only cancels changes made since the last save. jpayne@69: ''' jpayne@69: help_pages = { jpayne@69: 'Fonts/Tabs':''' jpayne@69: Font sample: This shows what a selection of Basic Multilingual Plane jpayne@69: unicode characters look like for the current font selection. If the jpayne@69: selected font does not define a character, Tk attempts to find another jpayne@69: font that does. Substitute glyphs depend on what is available on a jpayne@69: particular system and will not necessarily have the same size as the jpayne@69: font selected. Line contains 20 characters up to Devanagari, 14 for jpayne@69: Tamil, and 10 for East Asia. jpayne@69: jpayne@69: Hebrew and Arabic letters should display right to left, starting with jpayne@69: alef, \u05d0 and \u0627. Arabic digits display left to right. The jpayne@69: Devanagari and Tamil lines start with digits. The East Asian lines jpayne@69: are Chinese digits, Chinese Hanzi, Korean Hangul, and Japanese jpayne@69: Hiragana and Katakana. jpayne@69: jpayne@69: You can edit the font sample. Changes remain until IDLE is closed. jpayne@69: ''', jpayne@69: 'Highlights': ''' jpayne@69: Highlighting: jpayne@69: The IDLE Dark color theme is new in October 2015. It can only jpayne@69: be used with older IDLE releases if it is saved as a custom jpayne@69: theme, with a different name. jpayne@69: ''', jpayne@69: 'Keys': ''' jpayne@69: Keys: jpayne@69: The IDLE Modern Unix key set is new in June 2016. It can only jpayne@69: be used with older IDLE releases if it is saved as a custom jpayne@69: key set, with a different name. jpayne@69: ''', jpayne@69: 'General': ''' jpayne@69: General: jpayne@69: jpayne@69: AutoComplete: Popupwait is milliseconds to wait after key char, without jpayne@69: cursor movement, before popping up completion box. Key char is '.' after jpayne@69: identifier or a '/' (or '\\' on Windows) within a string. jpayne@69: jpayne@69: FormatParagraph: Max-width is max chars in lines after re-formatting. jpayne@69: Use with paragraphs in both strings and comment blocks. jpayne@69: jpayne@69: ParenMatch: Style indicates what is highlighted when closer is entered: jpayne@69: 'opener' - opener '({[' corresponding to closer; 'parens' - both chars; jpayne@69: 'expression' (default) - also everything in between. Flash-delay is how jpayne@69: long to highlight if cursor is not moved (0 means forever). jpayne@69: jpayne@69: CodeContext: Maxlines is the maximum number of code context lines to jpayne@69: display when Code Context is turned on for an editor window. jpayne@69: jpayne@69: Shell Preferences: Auto-Squeeze Min. Lines is the minimum number of lines jpayne@69: of output to automatically "squeeze". jpayne@69: ''' jpayne@69: } jpayne@69: jpayne@69: jpayne@69: def is_int(s): jpayne@69: "Return 's is blank or represents an int'" jpayne@69: if not s: jpayne@69: return True jpayne@69: try: jpayne@69: int(s) jpayne@69: return True jpayne@69: except ValueError: jpayne@69: return False jpayne@69: jpayne@69: jpayne@69: class VerticalScrolledFrame(Frame): jpayne@69: """A pure Tkinter vertically scrollable frame. jpayne@69: jpayne@69: * Use the 'interior' attribute to place widgets inside the scrollable frame jpayne@69: * Construct and pack/place/grid normally jpayne@69: * This frame only allows vertical scrolling jpayne@69: """ jpayne@69: def __init__(self, parent, *args, **kw): jpayne@69: Frame.__init__(self, parent, *args, **kw) jpayne@69: jpayne@69: # Create a canvas object and a vertical scrollbar for scrolling it. jpayne@69: vscrollbar = Scrollbar(self, orient=VERTICAL) jpayne@69: vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE) jpayne@69: canvas = Canvas(self, borderwidth=0, highlightthickness=0, jpayne@69: yscrollcommand=vscrollbar.set, width=240) jpayne@69: canvas.pack(side=LEFT, fill=BOTH, expand=TRUE) jpayne@69: vscrollbar.config(command=canvas.yview) jpayne@69: jpayne@69: # Reset the view. jpayne@69: canvas.xview_moveto(0) jpayne@69: canvas.yview_moveto(0) jpayne@69: jpayne@69: # Create a frame inside the canvas which will be scrolled with it. jpayne@69: self.interior = interior = Frame(canvas) jpayne@69: interior_id = canvas.create_window(0, 0, window=interior, anchor=NW) jpayne@69: jpayne@69: # Track changes to the canvas and frame width and sync them, jpayne@69: # also updating the scrollbar. jpayne@69: def _configure_interior(event): jpayne@69: # Update the scrollbars to match the size of the inner frame. jpayne@69: size = (interior.winfo_reqwidth(), interior.winfo_reqheight()) jpayne@69: canvas.config(scrollregion="0 0 %s %s" % size) jpayne@69: interior.bind('', _configure_interior) jpayne@69: jpayne@69: def _configure_canvas(event): jpayne@69: if interior.winfo_reqwidth() != canvas.winfo_width(): jpayne@69: # Update the inner frame's width to fill the canvas. jpayne@69: canvas.itemconfigure(interior_id, width=canvas.winfo_width()) jpayne@69: canvas.bind('', _configure_canvas) jpayne@69: jpayne@69: return jpayne@69: jpayne@69: jpayne@69: if __name__ == '__main__': jpayne@69: from unittest import main jpayne@69: main('idlelib.idle_test.test_configdialog', verbosity=2, exit=False) jpayne@69: jpayne@69: from idlelib.idle_test.htest import run jpayne@69: run(ConfigDialog)