Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dialog.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. import win32con
  7. import win32ui
  8. # sob - 2to3 doesn't see this as a relative import :(
  9. from pywin.mfc import window
  10. def dllFromDll(dllid):
  11. "given a 'dll' (maybe a dll, filename, etc), return a DLL object"
  12. if dllid == None:
  13. return None
  14. elif type("") == type(dllid):
  15. return win32ui.LoadLibrary(dllid)
  16. else:
  17. try:
  18. dllid.GetFileName()
  19. except AttributeError:
  20. raise TypeError("DLL parameter must be None, a filename or a dll object")
  21. return dllid
  22. class Dialog(window.Wnd):
  23. "Base class for a dialog"
  24. def __init__(self, id, dllid=None):
  25. """id is the resource ID, or a template
  26. dllid may be None, a dll object, or a string with a dll name"""
  27. # must take a reference to the DLL until InitDialog.
  28. self.dll = dllFromDll(dllid)
  29. if type(id) == type([]): # a template
  30. dlg = win32ui.CreateDialogIndirect(id)
  31. else:
  32. dlg = win32ui.CreateDialog(id, self.dll)
  33. window.Wnd.__init__(self, dlg)
  34. self.HookCommands()
  35. self.bHaveInit = None
  36. def HookCommands(self):
  37. pass
  38. def OnAttachedObjectDeath(self):
  39. self.data = self._obj_.data
  40. window.Wnd.OnAttachedObjectDeath(self)
  41. # provide virtuals.
  42. def OnOK(self):
  43. self._obj_.OnOK()
  44. def OnCancel(self):
  45. self._obj_.OnCancel()
  46. def OnInitDialog(self):
  47. self.bHaveInit = 1
  48. if self._obj_.data:
  49. self._obj_.UpdateData(0)
  50. return 1 # I did NOT set focus to a child window.
  51. def OnDestroy(self, msg):
  52. self.dll = None # theoretically not needed if object destructs normally.
  53. # DDX support
  54. def AddDDX(self, *args):
  55. self._obj_.datalist.append(args)
  56. # Make a dialog object look like a dictionary for the DDX support
  57. def __bool__(self):
  58. return True
  59. def __len__(self):
  60. return len(self.data)
  61. def __getitem__(self, key):
  62. return self.data[key]
  63. def __setitem__(self, key, item):
  64. self._obj_.data[key] = item # self.UpdateData(0)
  65. def keys(self):
  66. return list(self.data.keys())
  67. def items(self):
  68. return list(self.data.items())
  69. def values(self):
  70. return list(self.data.values())
  71. # XXX - needs py3k work!
  72. def has_key(self, key):
  73. return key in self.data
  74. class PrintDialog(Dialog):
  75. "Base class for a print dialog"
  76. def __init__(
  77. self,
  78. pInfo,
  79. dlgID,
  80. printSetupOnly=0,
  81. flags=(
  82. win32ui.PD_ALLPAGES
  83. | win32ui.PD_USEDEVMODECOPIES
  84. | win32ui.PD_NOPAGENUMS
  85. | win32ui.PD_HIDEPRINTTOFILE
  86. | win32ui.PD_NOSELECTION
  87. ),
  88. parent=None,
  89. dllid=None,
  90. ):
  91. self.dll = dllFromDll(dllid)
  92. if type(dlgID) == type([]): # a template
  93. raise TypeError("dlgID parameter must be an integer resource ID")
  94. dlg = win32ui.CreatePrintDialog(dlgID, printSetupOnly, flags, parent, self.dll)
  95. window.Wnd.__init__(self, dlg)
  96. self.HookCommands()
  97. self.bHaveInit = None
  98. self.pInfo = pInfo
  99. # init values (if PrintSetup is called, values still available)
  100. flags = pInfo.GetFlags()
  101. self["toFile"] = flags & win32ui.PD_PRINTTOFILE != 0
  102. self["direct"] = pInfo.GetDirect()
  103. self["preview"] = pInfo.GetPreview()
  104. self["continuePrinting"] = pInfo.GetContinuePrinting()
  105. self["curPage"] = pInfo.GetCurPage()
  106. self["numPreviewPages"] = pInfo.GetNumPreviewPages()
  107. self["userData"] = pInfo.GetUserData()
  108. self["draw"] = pInfo.GetDraw()
  109. self["pageDesc"] = pInfo.GetPageDesc()
  110. self["minPage"] = pInfo.GetMinPage()
  111. self["maxPage"] = pInfo.GetMaxPage()
  112. self["offsetPage"] = pInfo.GetOffsetPage()
  113. self["fromPage"] = pInfo.GetFromPage()
  114. self["toPage"] = pInfo.GetToPage()
  115. # these values updated after OnOK
  116. self["copies"] = 0
  117. self["deviceName"] = ""
  118. self["driverName"] = ""
  119. self["printAll"] = 0
  120. self["printCollate"] = 0
  121. self["printRange"] = 0
  122. self["printSelection"] = 0
  123. def OnInitDialog(self):
  124. self.pInfo.CreatePrinterDC() # This also sets the hDC of the pInfo structure.
  125. return self._obj_.OnInitDialog()
  126. def OnCancel(self):
  127. del self.pInfo
  128. def OnOK(self):
  129. """DoModal has finished. Can now access the users choices"""
  130. self._obj_.OnOK()
  131. pInfo = self.pInfo
  132. # user values
  133. flags = pInfo.GetFlags()
  134. self["toFile"] = flags & win32ui.PD_PRINTTOFILE != 0
  135. self["direct"] = pInfo.GetDirect()
  136. self["preview"] = pInfo.GetPreview()
  137. self["continuePrinting"] = pInfo.GetContinuePrinting()
  138. self["curPage"] = pInfo.GetCurPage()
  139. self["numPreviewPages"] = pInfo.GetNumPreviewPages()
  140. self["userData"] = pInfo.GetUserData()
  141. self["draw"] = pInfo.GetDraw()
  142. self["pageDesc"] = pInfo.GetPageDesc()
  143. self["minPage"] = pInfo.GetMinPage()
  144. self["maxPage"] = pInfo.GetMaxPage()
  145. self["offsetPage"] = pInfo.GetOffsetPage()
  146. self["fromPage"] = pInfo.GetFromPage()
  147. self["toPage"] = pInfo.GetToPage()
  148. self["copies"] = pInfo.GetCopies()
  149. self["deviceName"] = pInfo.GetDeviceName()
  150. self["driverName"] = pInfo.GetDriverName()
  151. self["printAll"] = pInfo.PrintAll()
  152. self["printCollate"] = pInfo.PrintCollate()
  153. self["printRange"] = pInfo.PrintRange()
  154. self["printSelection"] = pInfo.PrintSelection()
  155. del self.pInfo
  156. class PropertyPage(Dialog):
  157. "Base class for a Property Page"
  158. def __init__(self, id, dllid=None, caption=0):
  159. """id is the resource ID
  160. dllid may be None, a dll object, or a string with a dll name"""
  161. self.dll = dllFromDll(dllid)
  162. if self.dll:
  163. oldRes = win32ui.SetResource(self.dll)
  164. if type(id) == type([]):
  165. dlg = win32ui.CreatePropertyPageIndirect(id)
  166. else:
  167. dlg = win32ui.CreatePropertyPage(id, caption)
  168. if self.dll:
  169. win32ui.SetResource(oldRes)
  170. # dont call dialog init!
  171. window.Wnd.__init__(self, dlg)
  172. self.HookCommands()
  173. class PropertySheet(window.Wnd):
  174. def __init__(self, caption, dll=None, pageList=None): # parent=None, style,etc):
  175. "Initialize a property sheet. pageList is a list of ID's"
  176. # must take a reference to the DLL until InitDialog.
  177. self.dll = dllFromDll(dll)
  178. self.sheet = win32ui.CreatePropertySheet(caption)
  179. window.Wnd.__init__(self, self.sheet)
  180. if not pageList is None:
  181. self.AddPage(pageList)
  182. def OnInitDialog(self):
  183. return self._obj_.OnInitDialog()
  184. def DoModal(self):
  185. if self.dll:
  186. oldRes = win32ui.SetResource(self.dll)
  187. rc = self.sheet.DoModal()
  188. if self.dll:
  189. win32ui.SetResource(oldRes)
  190. return rc
  191. def AddPage(self, pages):
  192. if self.dll:
  193. oldRes = win32ui.SetResource(self.dll)
  194. try: # try list style access
  195. pages[0]
  196. isSeq = 1
  197. except (TypeError, KeyError):
  198. isSeq = 0
  199. if isSeq:
  200. for page in pages:
  201. self.DoAddSinglePage(page)
  202. else:
  203. self.DoAddSinglePage(pages)
  204. if self.dll:
  205. win32ui.SetResource(oldRes)
  206. def DoAddSinglePage(self, page):
  207. "Page may be page, or int ID. Assumes DLL setup"
  208. if type(page) == type(0):
  209. self.sheet.AddPage(win32ui.CreatePropertyPage(page))
  210. else:
  211. self.sheet.AddPage(page)
  212. # define some app utility functions.
  213. def GetSimpleInput(prompt, defValue="", title=None):
  214. """displays a dialog, and returns a string, or None if cancelled.
  215. args prompt, defValue='', title=main frames title"""
  216. # uses a simple dialog to return a string object.
  217. if title is None:
  218. title = win32ui.GetMainFrame().GetWindowText()
  219. # 2to3 insists on converting 'Dialog.__init__' to 'tkinter.dialog...'
  220. DlgBaseClass = Dialog
  221. class DlgSimpleInput(DlgBaseClass):
  222. def __init__(self, prompt, defValue, title):
  223. self.title = title
  224. DlgBaseClass.__init__(self, win32ui.IDD_SIMPLE_INPUT)
  225. self.AddDDX(win32ui.IDC_EDIT1, "result")
  226. self.AddDDX(win32ui.IDC_PROMPT1, "prompt")
  227. self._obj_.data["result"] = defValue
  228. self._obj_.data["prompt"] = prompt
  229. def OnInitDialog(self):
  230. self.SetWindowText(self.title)
  231. return DlgBaseClass.OnInitDialog(self)
  232. dlg = DlgSimpleInput(prompt, defValue, title)
  233. if dlg.DoModal() != win32con.IDOK:
  234. return None
  235. return dlg["result"]