home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Tools / IDE / Wcontrols.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  9.8 KB  |  402 lines

  1. import Ctl
  2. import Controls
  3. import Win
  4. import Wbase
  5. import Qd
  6. import Evt
  7.  
  8. class ControlWidget(Wbase.ClickableWidget):
  9.     
  10.     """Baseclass for all native controls."""
  11.     
  12.     def __init__(self, possize, title = "Control", procID = 0, callback = None, value = 0, min = 0, max = 1):
  13.         Wbase.ClickableWidget.__init__(self, possize)
  14.         self._control = None
  15.         self._title = title
  16.         self._callback = callback
  17.         self._procID = procID
  18.         self._value = value
  19.         self._min = min
  20.         self._max = max
  21.         self._enabled = 1
  22.     
  23.     def open(self):
  24.         self._calcbounds()
  25.         self._control = Ctl.NewControl(self._parentwindow.wid, 
  26.                         self._bounds, 
  27.                         self._title, 
  28.                         1, 
  29.                         self._value, 
  30.                         self._min, 
  31.                         self._max, 
  32.                         self._procID, 
  33.                         0)
  34.         self.SetPort()
  35.         #Win.ValidRect(self._bounds)
  36.         self.enable(self._enabled)
  37.     
  38.     def adjust(self, oldbounds):
  39.         self.SetPort()
  40.         self._control.HideControl()
  41.         self._control.MoveControl(self._bounds[0], self._bounds[1])
  42.         self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
  43.         if self._visible:
  44.             Qd.EraseRect(self._bounds)
  45.             self._control.ShowControl()
  46.             Win.ValidRect(self._bounds)
  47.     
  48.     def close(self):
  49.         self._control.HideControl()
  50.         self._control = None
  51.         Wbase.ClickableWidget.close(self)
  52.     
  53.     def enable(self, onoff):
  54.         if self._control and self._enabled <> onoff:
  55.             self._control.HiliteControl((not onoff) and 255)
  56.             self._enabled = onoff
  57.     
  58.     def show(self, onoff):
  59.         self._visible = onoff
  60.         for w in self._widgets:
  61.             w.show(onoff)
  62.         if onoff:
  63.             self._control.ShowControl()
  64.         else:
  65.             self._control.HideControl()
  66.     
  67.     def activate(self, onoff):
  68.         self._activated = onoff
  69.         if self._enabled:
  70.             self._control.HiliteControl((not onoff) and 255)
  71.     
  72.     def draw(self, visRgn = None):
  73.         if self._visible:
  74.             self._control.Draw1Control()
  75.     
  76.     def test(self, point):
  77.         ctltype, control = Ctl.FindControl(point, self._parentwindow.wid)
  78.         if self._enabled and control == self._control:
  79.             return 1
  80.     
  81.     def click(self, point, modifiers):
  82.         if not self._enabled:
  83.             return
  84.         part = self._control.TrackControl(point)
  85.         if part:
  86.             if self._callback:
  87.                 Wbase.CallbackCall(self._callback, 0)
  88.     
  89.     def settitle(self, title):
  90.         if self._control:
  91.             self._control.SetControlTitle(title)
  92.         self._title = title
  93.     
  94.     def gettitle(self):
  95.         return self._title
  96.  
  97. class Button(ControlWidget):
  98.     
  99.     """Standard push button."""
  100.     
  101.     def __init__(self, possize, title = "Button", callback = None):
  102.         procID = Controls.pushButProc | Controls.useWFont
  103.         ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
  104.         self._isdefault = 0
  105.     
  106.     def push(self):
  107.         if not self._enabled:
  108.             return
  109.         import time
  110.         self._control.HiliteControl(1)
  111.         time.sleep(0.1)
  112.         self._control.HiliteControl(0)
  113.         if self._callback:
  114.             Wbase.CallbackCall(self._callback, 0)
  115.     
  116.     def enable(self, onoff):
  117.         if self._control and self._enabled <> onoff:
  118.             self._control.HiliteControl((not onoff) and 255)
  119.             self._enabled = onoff
  120.             if self._isdefault and self._visible:
  121.                 self.SetPort()
  122.                 self.drawfatframe(onoff)
  123.     
  124.     def activate(self, onoff):
  125.         self._activated = onoff
  126.         if self._enabled:
  127.             self._control.HiliteControl((not onoff) and 255)
  128.             if self._isdefault and self._visible:
  129.                 self.SetPort()
  130.                 self.drawfatframe(onoff)
  131.     
  132.     def show(self, onoff):
  133.         ControlWidget.show(self, onoff)
  134.         if self._isdefault:
  135.             self.drawfatframe(onoff and self._enabled)
  136.     
  137.     def draw(self, visRgn = None):
  138.         if self._visible:
  139.             self._control.Draw1Control()
  140.             if self._isdefault and self._activated:
  141.                 self.drawfatframe(self._enabled)
  142.     
  143.     def drawfatframe(self, onoff):
  144.         state = Qd.GetPenState()
  145.         if onoff:
  146.             Qd.PenPat(Qd.qd.black)
  147.         else:
  148.             Qd.PenPat(Qd.qd.white)
  149.         fatrect = Qd.InsetRect(self._bounds, -4, -4)
  150.         Qd.PenSize(3, 3)
  151.         Qd.FrameRoundRect(fatrect, 16, 16)
  152.         Qd.SetPenState(state)
  153.     
  154.     def _setdefault(self, onoff):
  155.         self._isdefault = onoff
  156.         if self._control and self._enabled:
  157.             self.SetPort()
  158.             self.drawfatframe(onoff)
  159.     
  160.     def adjust(self, oldbounds):
  161.         if self._isdefault:
  162.             old = Qd.InsetRect(oldbounds, -4, -4)
  163.             new = Qd.InsetRect(self._bounds, -4, -4)
  164.             Qd.EraseRect(old)
  165.             Win.InvalRect(old)
  166.             Win.InvalRect(new)
  167.         ControlWidget.adjust(self, oldbounds)
  168.  
  169.  
  170. class CheckBox(ControlWidget):
  171.     
  172.     """Standard checkbox."""
  173.     
  174.     def __init__(self, possize, title = "Checkbox", callback = None, value = 0):
  175.         procID = Controls.checkBoxProc | Controls.useWFont
  176.         ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
  177.     
  178.     def click(self, point, modifiers):
  179.         if not self._enabled:
  180.             return
  181.         part = self._control.TrackControl(point)
  182.         if part:
  183.             self.toggle()
  184.             if self._callback:
  185.                 Wbase.CallbackCall(self._callback, 0, self.get())
  186.     
  187.     def push(self):
  188.         if not self._enabled:
  189.             return
  190.         self.toggle()
  191.         if self._callback:
  192.             Wbase.CallbackCall(self._callback, 0, self.get())
  193.     
  194.     def toggle(self):
  195.         self.set(not self.get())
  196.     
  197.     def set(self, value):
  198.         if self._control:
  199.             self._control.SetControlValue(value)
  200.         else:
  201.             self._value = value
  202.     
  203.     def get(self):
  204.         if self._control:
  205.             return self._control.GetControlValue()
  206.         else:
  207.             return self._value
  208.     
  209.  
  210. class RadioButton(ControlWidget):
  211.     
  212.     """Standard radiobutton."""
  213.     
  214.     # XXX We need a radiogroup widget; this is too kludgy.
  215.     
  216.     def __init__(self, possize, title, thebuttons, callback = None, value = 0):
  217.         procID = Controls.radioButProc | Controls.useWFont
  218.         ControlWidget.__init__(self, possize, title, procID, callback, value, 0, 1)
  219.         self.thebuttons = thebuttons
  220.         thebuttons.append(self)
  221.     
  222.     def close(self):
  223.         self.thebuttons = None
  224.         ControlWidget.close(self)
  225.     
  226.     def click(self, point, modifiers):
  227.         if not self._enabled:
  228.             return
  229.         part = self._control.TrackControl(point)
  230.         if part:
  231.             self.set(1)
  232.             if self._callback:
  233.                 Wbase.CallbackCall(self._callback, 0, 1)
  234.     
  235.     def push(self):
  236.         if not self._enabled:
  237.             return
  238.         self.set(1)
  239.         if self._callback:
  240.             Wbase.CallbackCall(self._callback, 0, 1)
  241.     
  242.     def set(self, value):
  243.         for button in self.thebuttons:
  244.             if button._control:
  245.                 button._control.SetControlValue(button == self)
  246.             else:
  247.                 button._value = (button == self)
  248.     
  249.     def get(self):
  250.         if self._control:
  251.             return self._control.GetControlValue()
  252.         else:
  253.             return self._value
  254.     
  255.  
  256. class Scrollbar(ControlWidget):
  257.     
  258.     """Standard scrollbar."""
  259.     
  260.     def __init__(self, possize, callback = None, value = 0, min = 0, max = 0):
  261.         procID = Controls.scrollBarProc
  262.         ControlWidget.__init__(self, possize, "", procID, callback, value, min, max)
  263.     
  264.     # interface
  265.     def set(self, value):
  266.         if self._callback:
  267.             Wbase.CallbackCall(self._callback, 1, value)
  268.     
  269.     def up(self):
  270.         if self._callback:
  271.             Wbase.CallbackCall(self._callback, 1, '+')
  272.     
  273.     def down(self):
  274.         if self._callback:
  275.             Wbase.CallbackCall(self._callback, 1, '-')
  276.     
  277.     def pageup(self):
  278.         if self._callback:
  279.             Wbase.CallbackCall(self._callback, 1, '++')
  280.     
  281.     def pagedown(self):
  282.         if self._callback:
  283.             Wbase.CallbackCall(self._callback, 1, '--')
  284.     
  285.     def setmin(self, min):
  286.         self._control.SetControlMinimum(min)
  287.     
  288.     def setmax(self, min):
  289.         self._control.SetControlMinimum(max)
  290.     
  291.     def getmin(self):
  292.         return self._control.GetControlMinimum()
  293.     
  294.     def getmax(self):
  295.         return self._control.GetControlMinimum()
  296.     
  297.     # internals
  298.     def click(self, point, modifiers):
  299.         if not self._enabled:
  300.             return
  301.         # custom TrackControl. A mousedown in a scrollbar arrow or page area should
  302.         # generate _control hits as long as the mouse is a) down, b) still in the same part
  303.         part = self._control.TestControl(point)
  304.         if Controls.inUpButton <= part <= Controls.inPageDown:    
  305.             self._control.HiliteControl(part)
  306.             self._hit(part)
  307.             oldpart = part
  308.             # slight delay before scrolling at top speed...
  309.             now = Evt.TickCount()
  310.             while Evt.StillDown():
  311.                 if (Evt.TickCount() - now) > 18: # 0.3 seconds
  312.                     break
  313.             while Evt.StillDown():
  314.                 part = self._control.TestControl(point)
  315.                 if part == oldpart:
  316.                     self._control.HiliteControl(part)
  317.                     self._hit(part)
  318.                 else:
  319.                     self._control.HiliteControl(0)
  320.                 self.SetPort()
  321.                 point = Evt.GetMouse()
  322.             self._control.HiliteControl(0)
  323.         elif part == Controls.inThumb:
  324.             part = self._control.TrackControl(point)
  325.             if part:
  326.                 self._hit(part)
  327.     
  328.     def _hit(self, part):
  329.         if part == Controls.inThumb:
  330.             value = self._control.GetControlValue()
  331.         elif part == Controls.inUpButton:
  332.             value = "+"
  333.         elif part == Controls.inDownButton:
  334.             value = "-"
  335.         elif part == Controls.inPageUp:
  336.             value = "++"
  337.         elif part == Controls.inPageDown:
  338.             value = "--"
  339.         if self._callback:
  340.             Wbase.CallbackCall(self._callback, 1, value)
  341.     
  342.     def draw(self, visRgn = None):
  343.         if self._visible:
  344.             self._control.Draw1Control()
  345.             Qd.FrameRect(self._bounds)
  346.     
  347.     def adjust(self, oldbounds):
  348.         self.SetPort()
  349.         Win.InvalRect(oldbounds)
  350.         self._control.HideControl()
  351.         self._control.MoveControl(self._bounds[0], self._bounds[1])
  352.         self._control.SizeControl(self._bounds[2] - self._bounds[0], self._bounds[3] - self._bounds[1])
  353.         if self._visible:
  354.             Qd.EraseRect(self._bounds)
  355.             if self._activated:
  356.                 self._control.ShowControl()
  357.             else:
  358.                 Qd.FrameRect(self._bounds)
  359.             Win.ValidRect(self._bounds)
  360.     
  361.     def activate(self, onoff):
  362.         self._activated = onoff
  363.         if self._visible:
  364.             if onoff:
  365.                 self._control.ShowControl()
  366.             else:
  367.                 self._control.HideControl()
  368.                 self.draw(None)
  369.                 Win.ValidRect(self._bounds)
  370.         
  371.     def set(self, value):
  372.         if self._control:
  373.             self._control.SetControlValue(value)
  374.         else:
  375.             self._value = value
  376.     
  377.     def get(self):
  378.         if self._control:
  379.             return self._control.GetControlValue()
  380.         else:
  381.             return self._value
  382.     
  383.  
  384. class __xxxx_PopupControl(ControlWidget):
  385.     
  386.     def __init__(self, possize, title = "Button", callback = None):
  387.         procID = Controls.popupMenuProc    # | Controls.useWFont
  388.         ControlWidget.__init__(self, possize, title, procID, callback, 0, 0, 1)
  389.         self._isdefault = 0
  390.     
  391.  
  392. def _scalebarvalue(absmin, absmax, curmin, curmax):
  393.     if curmin <= absmin and curmax >= absmax:
  394.         return None
  395.     if curmin <= absmin:
  396.         return 0
  397.     if curmax >= absmax:
  398.         return 32767
  399.     perc = float(curmin-absmin) / float((absmax - absmin) - (curmax - curmin))
  400.     return int(perc*32767)
  401.  
  402.