home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Lib / test / tlist_dialog.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  1.8 KB  |  80 lines

  1. from Dlg import *
  2. from Events import *
  3. from Evt import *
  4. from List import *
  5. from Qd import *
  6. import Res
  7. import string
  8. import MacOS
  9.  
  10. ID = 513
  11.  
  12. def dodialog(items):
  13.     print 'This is to create a window'
  14.     #
  15.     # Create the dialog
  16.     #
  17.     d = GetNewDialog(ID, -1)
  18.     #
  19.     # Create the list and fill it
  20.     #
  21.     tp, h, rect = d.GetDialogItem(2)
  22.     rect = rect[0], rect[1], rect[2]-15, rect[3]-15  # Space for scrollbars
  23.     length = (len(items)+1) / 2
  24.     list = LNew(rect, (0, 0, 2, length), (0, 0), 0, d, 0, 1, 1, 1)
  25.     for i in range(len(items)):
  26.         list.LSetCell(items[i], (i%2, i/2))
  27.     #
  28.     # Draw it.
  29.     #
  30.     list.LSetDrawingMode(1)
  31.     list.LUpdate(self.wid.GetWindowPort().visRgn)
  32.     #
  33.     # Do the (modeless) dialog
  34.     #
  35.     while 1:
  36.         ok, ev = WaitNextEvent(0xffff, 10)
  37.         if not ok:
  38.             # No event.
  39.             continue
  40.         (what, message, when, where, modifiers) = ev
  41.         if what == updateEvt:
  42.             # XXXX We just always update our list (sigh...)
  43.             SetPort(window)
  44.             list.LUpdate(self.wid.GetWindowPort().visRgn)
  45.         if IsDialogEvent(ev):
  46.             # It is a dialog event. See if it's ours.
  47.             ok, window, item = DialogSelect(ev)
  48.             if ok:
  49.                 if window == d:
  50.                     # Yes, it is ours.
  51.                     if item == 1:    # OK button
  52.                         break
  53.                     elif item == 2:    # List
  54.                         (what, message, when, where, modifiers) = ev
  55.                         SetPort(window)
  56.                         if what == mouseDown:
  57.                             local = GlobalToLocal(where)
  58.                             list.LClick(local, modifiers)
  59.                     else:
  60.                         print 'Unexpected item hit'
  61.                 else:
  62.                     print 'Unexpected dialog hit'
  63.         else:
  64.             MacOS.HandleEvent(ev)
  65.     sel = []
  66.     for i in range(len(items)):
  67.         ok, dummycell = list.LGetSelect(0, (i%2, i/2))
  68.         if ok:
  69.             sel.append(list.LGetCell(256, (i%2, i/2)))
  70.     print 'Your selection:', sel
  71.  
  72. def test():
  73.     import os, sys
  74.     Res.OpenResFile('tlist_dialog.rsrc')
  75.     dodialog(os.listdir(':'))
  76.     sys.exit(1)
  77.  
  78. if __name__ == '__main__':
  79.     test()
  80.