home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 1998 November / maximum-cd-1998-11.iso / Truespace 4 / Data / PROGRAM / Scripts / dialog.py < prev    next >
Encoding:
Python Source  |  1998-01-20  |  5.4 KB  |  174 lines

  1. """ \
  2. Base class for Dialogs.  Also contains a few useful utility functions
  3. """
  4. # dialog.py
  5. # Python class for Dialog Boxes in PythonWin.
  6.  
  7. import win32ui
  8. import win32con
  9. import window
  10.  
  11. def dllFromDll(dllid):
  12.     " given a 'dll' (maybe a dll, filename, etc), return a DLL object "
  13.     if dllid==None:
  14.         return None
  15.     elif type('')==type(dllid):
  16.         return win32ui.LoadLibrary(dllid)
  17.     else:
  18.         try:
  19.             dllid.GetFileName()
  20.         except AttributeError:
  21.             raise TypeError, "DLL parameter must be None, a filename or a dll object"
  22.         return dllid
  23.     
  24. class Dialog(window.Wnd):
  25.     " Base class for a dialog"
  26.     def __init__( self, id, dllid=None ):
  27.         """ id is the resource ID, or a template
  28.             dllid may be None, a dll object, or a string with a dll name """
  29.         # must take a reference to the DLL until InitDialog.
  30.         self.dll=dllFromDll(dllid)
  31.         if type(id)==type([]):    # a template
  32.             dlg=win32ui.CreateDialogIndirect(id)
  33.         else:
  34.             dlg=win32ui.CreateDialog(id, self.dll)
  35.         window.Wnd.__init__(self, dlg)
  36.         self.HookCommands()
  37.         self.bHaveInit = None
  38.         
  39.     def HookCommands(self):
  40.         pass
  41.         
  42.     def OnAttachedObjectDeath(self):
  43.         self.data = self._obj_.data
  44.         window.Wnd.OnAttachedObjectDeath(self)
  45.  
  46.     # provide virtuals.
  47.     def OnOK(self):
  48.         self._obj_.OnOK()
  49.     def OnCancel(self):
  50.         self._obj_.OnCancel()
  51.     def OnInitDialog(self):
  52.         self.bHaveInit = 1
  53.         if self._obj_.data:
  54.             self._obj_.UpdateData(0)
  55.         return 1         # I did NOT set focus to a child window.
  56.     def OnDestroy(self,msg):
  57.         self.dll = None     # theoretically not needed if object destructs normally.
  58.     # DDX support
  59.     def AddDDX( self, *args ):
  60.         self._obj_.datalist.append(args)
  61.     # Make a dialog object look like a dictionary for the DDX support
  62.     def __nonzero__(self):
  63.         return 1
  64.     def __len__(self): return len(self.data)
  65.     def __getitem__(self, key): return self.data[key]
  66.     def __setitem__(self, key, item): self._obj_.data[key] = item# self.UpdateData(0)
  67.     def keys(self): return self.data.keys()
  68.     def items(self): return self.data.items()
  69.     def values(self): return self.data.values()
  70.     def has_key(self, key): return self.data.has_key(key)
  71.  
  72. class PropertyPage(Dialog):
  73.     " Base class for a Property Page"
  74.     def __init__( self, id, dllid=None, caption=0 ):
  75.         """ id is the resource ID
  76.             dllid may be None, a dll object, or a string with a dll name """
  77.     
  78.         self.dll = dllFromDll(dllid)
  79.         if self.dll:
  80.             oldRes = win32ui.SetResource(self.dll)
  81.         if type(id)==type([]):
  82.             dlg=win32ui.CreatePropertyPageIndirect(id)
  83.         else:
  84.             dlg=win32ui.CreatePropertyPage(id, caption)
  85.         if self.dll:
  86.             win32ui.SetResource(oldRes)
  87.         # dont call dialog init!
  88.         window.Wnd.__init__(self, dlg)
  89.         self.HookCommands()
  90.  
  91.     # DDX support
  92. #    def AddDDX( self, *args ):
  93. #        self._obj_.datalist.append(args)
  94.     # Make a property page object look like a dictionary for the DDX support
  95. #    def __nonzero__(self):
  96. #        return 1
  97. #    def __len__(self): return len(self._obj_.data)
  98. #    def __getitem__(self, key): return self._obj_.data[key]
  99. #    def __setitem__(self, key, item): self._obj_.data[key] = item # self.sheet.UpdateData(0)
  100. #    def __delitem__(self, key): del self._obj_.data[key]
  101. #    def keys(self): return self._obj_.data.keys()
  102. #    def items(self): return self._obj_.data.items()
  103. #    def values(self): return self._obj_.data.values()
  104. #    def has_key(self, key): return self._obj_.data.has_key(key)
  105.  
  106. class PropertySheet(window.Wnd):
  107.     def __init__(self, caption, dll=None, pageList=None ):# parent=None, style,etc):
  108.         " Initialize a property sheet.  pageList is a list of ID's "
  109.         # must take a reference to the DLL until InitDialog.
  110.         self.dll=dllFromDll(dll)
  111.         self.sheet = win32ui.CreatePropertySheet(caption)
  112.         window.Wnd.__init__(self, self.sheet)
  113.         if not pageList is None:
  114.             self.AddPage(pageList)
  115.  
  116.     def OnInitDialog(self):
  117.         return self._obj_.OnInitDialog()
  118.                     
  119.     def DoModal(self):
  120.         if self.dll:
  121.             oldRes = win32ui.SetResource(self.dll)
  122.         rc = self.sheet.DoModal()
  123.         if self.dll:
  124.             win32ui.SetResource(oldRes)
  125.         return rc
  126.         
  127.     def AddPage(self, pages):
  128.         if self.dll:
  129.             oldRes = win32ui.SetResource(self.dll)
  130.         try:    # try list style access        
  131.             pages[0]
  132.             isSeq = 1
  133.         except (TypeError,KeyError):
  134.             isSeq = 0
  135.         if isSeq:
  136.             for page in pages:
  137.                 self.DoAddSinglePage(page)
  138.         else:
  139.             self.DoAddSinglePage(pages)
  140.         if self.dll:
  141.             win32ui.SetResource(oldRes)
  142.         
  143.     def DoAddSinglePage(self, page):
  144.         "Page may be page, or int ID. Assumes DLL setup "
  145.         if type(page)==type(0):
  146.             self.sheet.AddPage(win32ui.CreatePropertyPage(page))
  147.         else:
  148.             self.sheet.AddPage(page)
  149.         
  150. # define some app utility functions.
  151. def GetSimpleInput(prompt, defValue='', title=None ):
  152.     """ displays a dialog, and returns a string, or None if cancelled.
  153.     args prompt, defValue='', title=main frames title """
  154.     # uses a simple dialog to return a string object.
  155.     if title is None: title=win32ui.GetMainFrame().GetWindowText()
  156.     class DlgSimpleInput(Dialog):
  157.         def __init__(self, prompt, defValue, title ):
  158.             self.title=title
  159.             Dialog.__init__(self, win32ui.IDD_SIMPLE_INPUT)
  160.             self.AddDDX(win32ui.IDC_EDIT1,'result')
  161.             self.AddDDX(win32ui.IDC_PROMPT1, 'prompt')
  162.             self._obj_.data['result']=defValue
  163.             self._obj_.data['prompt']=prompt
  164.         def OnInitDialog(self):
  165.             self.SetWindowText(self.title)
  166.             return Dialog.OnInitDialog(self)
  167.             
  168.     dlg=DlgSimpleInput( prompt, defValue, title)
  169.     if dlg.DoModal() <> win32con.IDOK:
  170.         return None
  171.     return dlg['result']
  172.  
  173.  
  174.