From ccc2ea931999b859d46cfebf7dffe72a10616eba Mon Sep 17 00:00:00 2001 From: Elvish_Hunter Date: Tue, 28 Jul 2015 11:41:58 +0200 Subject: [PATCH] GUI.pyw: remove some duplicated code --- data/tools/GUI.pyw | 104 ++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 62 deletions(-) diff --git a/data/tools/GUI.pyw b/data/tools/GUI.pyw index cebe8ab783bb..ac19eb91283d 100755 --- a/data/tools/GUI.pyw +++ b/data/tools/GUI.pyw @@ -95,6 +95,42 @@ def is_wesnoth_tools_path(path): return True return False +def attach_context_menu(widget,function): + # on Mac the right button fires a Button-2 event, or so I'm told + # some mice don't even have two buttons, so the user is forced + # to use Command + the only button + # bear in mind that I don't have a Mac, so this point may be bugged + # bind also the context menu key, for those keyboards that have it + # that is, most of the Windows and Linux ones (however, in Win it's + # called App, while on Linux is called Menu) + # Mac doesn't have a context menu key on its keyboards, so no binding + # bind also the Shift+F10 shortcut (same as Menu/App key) + # the call to tk windowingsystem is justified by the fact + # that it is possible to install X11 over Darwin + windowingsystem = widget.tk.call('tk', 'windowingsystem') + if windowingsystem == "win32": # Windows, both 32 and 64 bit + widget.bind("", function) + widget.bind("", function) + widget.bind("", function) + elif windowingsystem == "aqua": # MacOS with Aqua + widget.bind("", function) + widget.bind("", function) + elif windowingsystem == "x11": # Linux, FreeBSD, Darwin with X11 + widget.bind("", function) + widget.bind("", function) + widget.bind("", function) + +def attach_select_all(widget,function): + # bind the "select all" key shortcut + # again, Mac uses Command instead of Control + windowingsystem = widget.tk.call('tk', 'windowingsystem') + if windowingsystem == "win32": + widget.bind("", function) + elif windowingsystem == "aqua": + widget.bind("", function) + elif windowingsystem == "x11": + widget.bind("", function) + class Popup(Toplevel): def __init__(self,parent,tool,thread): """Creates a popup that informs the user that the desired tool is running. @@ -205,36 +241,8 @@ Use like any other Entry widget""" super().__init__(parent,**kwargs) else: Entry.__init__(self,parent,**kwargs) - # on Mac the right button fires a Button-2 event, or so I'm told - # some mice don't even have two buttons, so the user is forced - # to use Command + the only button - # bear in mind that I don't have a Mac, so this point may be bugged - # bind also the context menu key, for those keyboards that have it - # that is, most of the Windows and Linux ones (however, in Win it's - # called App, while on Linux is called Menu) - # Mac doesn't have a context menu key on its keyboards, so no binding - # bind also the Shift+F10 shortcut (same as Menu/App key) - # the call to tk windowingsystem is justified by the fact - # that it is possible to install X11 over Darwin - # finally, bind the "select all" key shortcut - # again, Mac uses Command instead of Control - windowingsystem = self.tk.call('tk', 'windowingsystem') - if windowingsystem == "win32": # Windows, both 32 and 64 bit - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - # for some weird reason, using a KeyPress binding to set the selection on - # a readonly Entry or disabled Text doesn't work, but a KeyRelease does - self.bind("", self.on_select_all) - elif windowingsystem == "aqua": # MacOS with Aqua - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) - elif windowingsystem == "x11": # Linux, FreeBSD, Darwin with X11 - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) + attach_context_menu(self,self.on_context_menu) + attach_select_all(self,self.on_select_all) def on_context_menu(self,event): if str(self.cget('state')) != DISABLED: ContextMenu(event.x_root,event.y_root,event.widget) @@ -249,22 +257,8 @@ Use like any other Spinbox widget""" super().__init__(parent,**kwargs) else: Spinbox.__init__(self,parent,**kwargs) - # see the above widget for an explanation of this block - windowingsystem = self.tk.call('tk', 'windowingsystem') - if windowingsystem == "win32": - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) - elif windowingsystem == "aqua": - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) - elif windowingsystem == "x11": - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) + attach_context_menu(self,self.on_context_menu) + attach_select_all(self,self.on_select_all) def on_context_menu(self,event): if str(self.cget('state')) != DISABLED: ContextMenu(event.x_root,event.y_root,event.widget) @@ -279,22 +273,8 @@ Use it like any other Text widget""" super().__init__(*args,**kwargs) else: Text.__init__(self,*args,**kwargs) - # see descriptions of above widgets - windowingsystem = self.tk.call('tk', 'windowingsystem') - if windowingsystem == "win32": # Windows, both 32 and 64 bit - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) - elif windowingsystem == "aqua": # MacOS with Aqua - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) - elif windowingsystem == "x11": # Linux, FreeBSD, Darwin with X11 - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("",self.on_context_menu) - self.bind("", self.on_select_all) + attach_context_menu(self,self.on_context_menu) + attach_select_all(self,self.on_select_all) def on_context_menu(self,event): # the disabled state in a Text widget is pretty much # like the readonly state in Entry, hence no state check