home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Lib / EasyDialogs.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  12.9 KB  |  362 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 1.5)
  3.  
  4. '''Easy to use dialogs.
  5.  
  6. Message(msg) -- display a message and an OK button.
  7. AskString(prompt, default) -- ask for a string, display OK and Cancel buttons.
  8. AskPassword(prompt, default) -- like AskString(), but shows text as bullets.
  9. AskYesNoCancel(question, default) -- display a question and Yes, No and Cancel buttons.
  10. bar = Progress(label, maxvalue) -- Display a progress bar
  11. bar.set(value) -- Set value
  12. bar.inc( *amount ) -- increment value by amount (default=1)
  13. bar.label( *newlabel ) -- get or set text label. 
  14.  
  15. More documentation in each function.
  16. This module uses DLOG resources 256, 257 and 258.
  17. Based upon STDWIN dialogs with the same names and functions.
  18. '''
  19. ModalDialog
  20. import Qd
  21. import QuickDraw
  22. import Dialogs
  23. import Windows
  24. import Dlg
  25. import Win
  26. import Evt
  27. import Events
  28. import MacOS
  29. import string
  30.  
  31. def cr2lf(text):
  32.     if '\r' in text:
  33.         text = string.join(string.split(text, '\r'), '\n')
  34.     
  35.     return text
  36.  
  37.  
  38. def lf2cr(text):
  39.     if '\n' in text:
  40.         text = string.join(string.split(text, '\n'), '\r')
  41.     
  42.     if len(text) > 253:
  43.         text = text[:253] + '\xc9'
  44.     
  45.     return text
  46.  
  47.  
  48. def Message(msg, id = 256, ok = None):
  49.     '''Display a MESSAGE string.
  50. \t
  51. \tReturn when the user clicks the OK button or presses Return.
  52. \t
  53. \tThe MESSAGE string can be at most 255 characters long.
  54. \t'''
  55.     d = GetNewDialog(id, -1)
  56.     if not d:
  57.         print "Can't get DLOG resource with id =", id
  58.         return None
  59.     
  60.     (tp, h, rect) = d.GetDialogItem(2)
  61.     SetDialogItemText(h, lf2cr(msg))
  62.     if ok != None:
  63.         (tp, h, rect) = d.GetDialogItem(1)
  64.         h.as_Control().SetControlTitle(ok)
  65.     
  66.     d.SetDialogDefaultItem(1)
  67.     while 1:
  68.         n = ModalDialog(None)
  69.         if n == 1:
  70.             return None
  71.         
  72.  
  73.  
  74. def AskString(prompt, default = '', id = 257, ok = None, cancel = None):
  75.     '''Display a PROMPT string and a text entry field with a DEFAULT string.
  76. \t
  77. \tReturn the contents of the text entry field when the user clicks the
  78. \tOK button or presses Return.
  79. \tReturn None when the user clicks the Cancel button.
  80. \t
  81. \tIf omitted, DEFAULT is empty.
  82. \t
  83. \tThe PROMPT and DEFAULT strings, as well as the return value,
  84. \tcan be at most 255 characters long.
  85. \t'''
  86.     id = 257
  87.     d = GetNewDialog(id, -1)
  88.     if not d:
  89.         print "Can't get DLOG resource with id =", id
  90.         return None
  91.     
  92.     (tp, h, rect) = d.GetDialogItem(3)
  93.     SetDialogItemText(h, lf2cr(prompt))
  94.     (tp, h, rect) = d.GetDialogItem(4)
  95.     SetDialogItemText(h, lf2cr(default))
  96.     d.SelectDialogItemText(4, 0, 999)
  97.     if ok != None:
  98.         (tp, h, rect) = d.GetDialogItem(1)
  99.         h.as_Control().SetControlTitle(ok)
  100.     
  101.     if cancel != None:
  102.         (tp, h, rect) = d.GetDialogItem(2)
  103.         h.as_Control().SetControlTitle(cancel)
  104.     
  105.     d.SetDialogDefaultItem(1)
  106.     d.SetDialogCancelItem(2)
  107.     while 1:
  108.         n = ModalDialog(None)
  109.         if n == 1:
  110.             (tp, h, rect) = d.GetDialogItem(4)
  111.             return cr2lf(GetDialogItemText(h))
  112.         
  113.         if n == 2:
  114.             return None
  115.         
  116.  
  117.  
  118. def AskPassword(prompt, default = '', id = 257):
  119.     '''Display a PROMPT string and a text entry field with a DEFAULT string.
  120. \tThe string is displayed as bullets only.
  121. \t
  122. \tReturn the contents of the text entry field when the user clicks the
  123. \tOK button or presses Return.
  124. \tReturn None when the user clicks the Cancel button.
  125. \t
  126. \tIf omitted, DEFAULT is empty.
  127. \t
  128. \tThe PROMPT and DEFAULT strings, as well as the return value,
  129. \tcan be at most 255 characters long.
  130. \t'''
  131.     d = GetNewDialog(id, -1)
  132.     if not d:
  133.         print "Can't get DLOG resource with id =", id
  134.         return None
  135.     
  136.     (tp, h, rect) = d.GetDialogItem(3)
  137.     SetDialogItemText(h, lf2cr(prompt))
  138.     (tp, h, rect) = d.GetDialogItem(4)
  139.     bullets = '\xa5' * len(default)
  140.     SetDialogItemText(h, bullets)
  141.     d.SelectDialogItemText(4, 999, 999)
  142.     d.SetDialogDefaultItem(Dialogs.ok)
  143.     d.SetDialogCancelItem(Dialogs.cancel)
  144.     string = default
  145.     oldschedparams = MacOS.SchedParams(0, 0)
  146.     while 1:
  147.         (ready, ev) = Evt.WaitNextEvent(Events.everyEvent, 6)
  148.         if not ready:
  149.             continue
  150.         
  151.         (what, msg, when, where, mod) = ev
  152.         if what == 0:
  153.             Dlg.DialogSelect(ev)
  154.         elif Dlg.IsDialogEvent(ev):
  155.             if what in (Events.keyDown, Events.autoKey):
  156.                 charcode = msg & Events.charCodeMask
  157.                 if mod & Events.cmdKey:
  158.                     MacOS.SysBeep()
  159.                     continue
  160.                 elif charcode == Events.kReturnCharCode:
  161.                     break
  162.                 elif charcode == Events.kEscapeCharCode:
  163.                     string = None
  164.                     break
  165.                 elif charcode in (Events.kLeftArrowCharCode, Events.kBackspaceCharCode):
  166.                     string = string[:-1]
  167.                 else:
  168.                     string = string + chr(charcode)
  169.                     msg = 165
  170.                     ev = (what, msg, when, where, mod)
  171.             
  172.             (rs, win, item) = Dlg.DialogSelect(ev)
  173.             if item == Dialogs.ok:
  174.                 break
  175.             elif item == Dialogs.cancel:
  176.                 string = None
  177.                 break
  178.             
  179.         elif what == Events.mouseDown:
  180.             (part, win) = Win.FindWindow(where)
  181.             if part == Windows.inDrag and win:
  182.                 win.DragWindow(where, screenbounds)
  183.             elif part == Windows.inMenuBar:
  184.                 MacOS.HandleEvent(ev)
  185.             else:
  186.                 MacOS.SysBeep()
  187.         elif what == Events.updateEvt:
  188.             MacOS.HandleEvent(ev)
  189.         
  190.     apply(MacOS.SchedParams, oldschedparams)
  191.     return string
  192.  
  193.  
  194. def AskYesNoCancel(question, default = 0, yes = None, no = None, cancel = None, id = 258):
  195.     '''Display a QUESTION string which can be answered with Yes or No.
  196. \t
  197. \tReturn 1 when the user clicks the Yes button.
  198. \tReturn 0 when the user clicks the No button.
  199. \tReturn -1 when the user clicks the Cancel button.
  200. \t
  201. \tWhen the user presses Return, the DEFAULT value is returned.
  202. \tIf omitted, this is 0 (No).
  203. \t
  204. \tThe QUESTION strign ca be at most 255 characters.
  205. \t'''
  206.     d = GetNewDialog(id, -1)
  207.     if not d:
  208.         print "Can't get DLOG resource with id =", id
  209.         return None
  210.     
  211.     (tp, h, rect) = d.GetDialogItem(5)
  212.     SetDialogItemText(h, lf2cr(question))
  213.     if yes != None:
  214.         (tp, h, rect) = d.GetDialogItem(2)
  215.         h.as_Control().SetControlTitle(yes)
  216.     
  217.     if no != None:
  218.         (tp, h, rect) = d.GetDialogItem(3)
  219.         h.as_Control().SetControlTitle(no)
  220.     
  221.     if cancel != None:
  222.         if cancel == '':
  223.             d.HideDialogItem(4)
  224.         else:
  225.             (tp, h, rect) = d.GetDialogItem(4)
  226.             h.as_Control().SetControlTitle(cancel)
  227.     
  228.     d.SetDialogCancelItem(4)
  229.     if default == 1:
  230.         d.SetDialogDefaultItem(2)
  231.     elif default == 0:
  232.         d.SetDialogDefaultItem(3)
  233.     elif default == -1:
  234.         d.SetDialogDefaultItem(4)
  235.     
  236.     while 1:
  237.         n = ModalDialog(None)
  238.         if n == 1:
  239.             return default
  240.         
  241.         if n == 2:
  242.             return 1
  243.         
  244.         if n == 3:
  245.             return 0
  246.         
  247.         if n == 4:
  248.             return -1
  249.         
  250.  
  251. screenbounds = Qd.qd.screenBits.bounds
  252. screenbounds = (screenbounds[0] + 4, screenbounds[1] + 4, screenbounds[2] - 4, screenbounds[3] - 4)
  253.  
  254. class ProgressBar:
  255.     
  256.     def __init__(self, title = 'Working...', maxval = 100, label = '', id = 259):
  257.         self.maxval = maxval
  258.         self.curval = -1
  259.         self.d = GetNewDialog(id, -1)
  260.         self.title(title)
  261.         self.label(label)
  262.         self._update(0)
  263.  
  264.     
  265.     def __del__(self):
  266.         self.d.BringToFront()
  267.         self.d.HideWindow()
  268.         del self.d
  269.  
  270.     
  271.     def title(self, newstr = ''):
  272.         '''title(text) - Set title of progress window'''
  273.         self.d.BringToFront()
  274.         w = self.d.GetDialogWindow()
  275.         w.SetWTitle(newstr)
  276.  
  277.     
  278.     def label(self, *newstr):
  279.         '''label(text) - Set text in progress box'''
  280.         self.d.BringToFront()
  281.         if newstr:
  282.             self._label = lf2cr(newstr[0])
  283.         
  284.         (tp, text_h, rect) = self.d.GetDialogItem(2)
  285.         SetDialogItemText(text_h, self._label)
  286.  
  287.     
  288.     def _update(self, value):
  289.         self.d.BringToFront()
  290.         (tp, h, bar_rect) = self.d.GetDialogItem(3)
  291.         Qd.SetPort(self.d)
  292.         Qd.FrameRect(bar_rect)
  293.         inner_rect = Qd.InsetRect(bar_rect, 1, 1)
  294.         (l, t, r, b) = inner_rect
  295.         Qd.ForeColor(QuickDraw.blackColor)
  296.         Qd.BackColor(QuickDraw.blackColor)
  297.         Qd.PaintRect((l, t, int(l + (r - l) * value / self.maxval), b))
  298.         Qd.ForeColor(QuickDraw.whiteColor)
  299.         Qd.BackColor(QuickDraw.whiteColor)
  300.         Qd.PaintRect((int(l + (r - l) * value / self.maxval), t, r, b))
  301.         Qd.ForeColor(QuickDraw.blackColor)
  302.         Qd.BackColor(QuickDraw.whiteColor)
  303.         (ready, ev) = Evt.WaitNextEvent(Events.mDownMask, 1)
  304.         if ready:
  305.             (what, msg, when, where, mod) = ev
  306.             part = Win.FindWindow(where)[0]
  307.             if Dlg.IsDialogEvent(ev):
  308.                 ds = Dlg.DialogSelect(ev)
  309.                 if ds[0] and ds[1] == self.d and ds[-1] == 1:
  310.                     raise KeyboardInterrupt, ev
  311.                 
  312.             elif part == 4:
  313.                 self.d.DragWindow(where, screenbounds)
  314.             else:
  315.                 MacOS.HandleEvent(ev)
  316.         
  317.  
  318.     
  319.     def set(self, value):
  320.         '''set(value) - Set progress bar position'''
  321.         if value < 0:
  322.             value = 0
  323.         
  324.         if value > self.maxval:
  325.             value = self.maxval
  326.         
  327.         self.curval = value
  328.         self._update(value)
  329.  
  330.     
  331.     def inc(self, n = 1):
  332.         '''inc(amt) - Increment progress bar position'''
  333.         self.set(self.curval + n)
  334.  
  335.  
  336.  
  337. def test():
  338.     import time
  339.     Message('Testing EasyDialogs.')
  340.     ok = AskYesNoCancel('Do you want to proceed?')
  341.     ok = AskYesNoCancel('Do you want to identify?', yes = 'Indentify', no = "Don't identify")
  342.     if ok > 0:
  343.         s = AskString('Enter your first name', 'Joe')
  344.         Message('Thank you,\n%s' % `s`)
  345.     
  346.     text = ('Working Hard...', 'Hardly Working...', 'So far, so good!', "Keep on truckin'")
  347.     bar = ProgressBar('Progress, progress...', 100)
  348.     
  349.     try:
  350.         appsw = MacOS.SchedParams(1, 0)
  351.         for i in range(100):
  352.             bar.set(i)
  353.             time.sleep(0.1)
  354.         
  355.         bar.label('Done.')
  356.         time.sleep(0.3)
  357.     finally:
  358.         del bar
  359.         apply(MacOS.SchedParams, appsw)
  360.  
  361.  
  362.