/ lib / python / nf.py.in
nf.py.in
  1  #    Copyright 2004, 2005, 2006 Jeff Epler <jepler@unpythonic.net>
  2  #
  3  #    This program is free software; you can redistribute it and/or modify
  4  #    it under the terms of the GNU General Public License as published by
  5  #    the Free Software Foundation; either version 2 of the License, or
  6  #    (at your option) any later version.
  7  #
  8  #    This program is distributed in the hope that it will be useful,
  9  #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11  #    GNU General Public License for more details.
 12  #
 13  #    You should have received a copy of the GNU General Public License
 14  #    along with this program; if not, write to the Free Software
 15  #    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 16  
 17  import os
 18  import Tkinter
 19  
 20  __all__ = 'makecommand makevar makebool makewidget start'.split()
 21  
 22  class _Emptyclass: pass
 23  
 24  def newinstance(klass):
 25      value = _Emptyclass()
 26      value.__class__ = klass
 27      return value
 28  
 29  # Tkinter.py rev 65400 introduces a change which makes Menu.delete blow
 30  # chunks.  Until this is resolved, monkeypatch it
 31  def delete(self, index1, index2=None):
 32      self.tk.call(self._w, 'delete', index1, index2)
 33  Tkinter.Menu.delete = delete
 34  
 35  class TclCommands:
 36      def __init__(self, master):
 37          for k, v in self.__class__.__dict__.items():
 38              if k.startswith("__") and k.endswith("__"): continue
 39              makecommand(master, v.__name__, v)
 40              setattr(self, k, v)
 41  
 42  class Variables:
 43      def __init__(self, master, *variables):
 44          for (name, klass) in variables:
 45              setattr(self, name, makevar(master, name, klass))
 46  
 47  class Widgets:
 48      def __init__(self, master, *widgets):
 49          for (name, klass, path) in widgets:
 50              setattr(self, name, makewidget(master, klass, path))
 51  
 52  def makecommand(master, name, func, subst=None, needcleanup=0):
 53      f = Tkinter.CallWrapper(func, subst, master).__call__
 54      master.tk.createcommand(name, f)
 55      if needcleanup:
 56          if master._tclCommands is None:
 57              master._tclCommands = []
 58          master._tclCommands.append(name)
 59      return name
 60  
 61  def makevar(master, name, klass, *default):
 62      self = newinstance(klass)
 63      self._master = master
 64      self._tk = master.tk
 65      self._name = name
 66      if default:
 67          self.set(default[0])
 68      else:
 69          self.set(self._default)
 70      return self
 71  
 72  def makebool(master, name, *default):
 73      return makevar(master, name, Tkinter.BooleanVar, *default)
 74  def makeint(master, name, *default):
 75      return makevar(master, name, Tkinter.IntVar, *default)
 76  def makefloat(master, name, *default):
 77      return makevar(master, name, Tkinter.DoubleVar, *default)
 78  def makestring(master, name, *default):
 79      return makevar(master, name, Tkinter.StringVar, *default)
 80  
 81  def makewidget(master, klass, path):
 82      path = str(path)
 83      self = newinstance(klass)
 84      if self._tclCommands is None:
 85          self._tclCommands = []
 86      if path[0] == '.':
 87          if master._w == '.': self._name = path[1:]
 88          else: self._name = path[len(master._w)+1:]
 89          self._w = path
 90      else:
 91          self._name = path
 92          if master._w == '.': self._w = '.' + path
 93          else: self._w = master._w + '.' + path
 94      self.children = {}
 95      master.children[self._name] = self
 96      self.master = master
 97      self.tk = master.tk
 98      return self
 99   
100  def find_prefix(f):
101      s = os.path.join(f, "share")
102      if os.path.exists(s): return f
103      if f == "/" or f == '': raise RuntimeError, "Share directory not found"
104      return find_prefix(os.path.dirname(f))
105  
106  PREFIX = "@prefix@"
107  SHARE = os.path.join(PREFIX, "share", "axis")
108  tcl_libdir = os.path.join(SHARE, "tcl")
109  
110  def source_lib_tcl(r, f):
111      r.tk.call("source", os.path.join(tcl_libdir, f))
112  
113  def start(r):
114      r.tk.call("set", "imagedir", os.path.join(SHARE, "images"))
115      r.tk.call("lappend", "auto_path", os.path.join(tcl_libdir, "bwidget"))
116      r.tk.call("lappend", "auto_path", "/usr/lib")
117  
118      source_lib_tcl(r, "accel.tcl")
119      source_lib_tcl(r, "support.tcl")
120      source_lib_tcl(r, "combobox.tcl")
121      source_lib_tcl(r, "dialog.tcl")
122  
123      r.tk.call("package", "require", "BWidget", "1.7")
124      r.tk.call("namespace", "import", "combobox::combobox")
125  
126  # vim:ts=8:sts=4:et: