home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / lib-stdwin / WindowParent.py < prev    next >
Text File  |  2000-12-21  |  5KB  |  190 lines

  1. # A 'WindowParent' is the only module that uses real stdwin functionality.
  2. # It is the root of the tree.
  3. # It should have exactly one child when realized.
  4. #
  5. # There is also an alternative interface to "mainloop" here.
  6.  
  7. import stdwin
  8. from stdwinevents import *
  9. import mainloop
  10.  
  11. from TransParent import ManageOneChild
  12.  
  13. Error = 'WindowParent.Error'    # Exception
  14.  
  15. class WindowParent(ManageOneChild):
  16.     #
  17.     def create(self, title, size):
  18.         self.title = title
  19.         self.size = size        # (width, height)
  20.         self._reset()
  21.         self.close_hook = WindowParent.delayed_destroy
  22.         return self
  23.     #
  24.     def _reset(self):
  25.         self.child = None
  26.         self.win = None
  27.         self.itimer = 0
  28.         self.do_mouse = 0
  29.         self.do_keybd = 0
  30.         self.do_timer = 0
  31.         self.do_altdraw = 0
  32.         self.pending_destroy = 0
  33.         self.close_hook = None
  34.         self.menu_hook = None
  35.     #
  36.     def destroy(self):
  37.         mainloop.unregister(self.win)
  38.         if self.child: self.child.destroy()
  39.         self._reset()
  40.     #
  41.     def delayed_destroy(self):
  42.         # This interface to be used by 'Close' buttons etc.;
  43.         # destroying a window from within a button hook
  44.         # is not a good idea...
  45.         self.pending_destroy = 1
  46.     #
  47.     def close_trigger(self):
  48.         if self.close_hook: self.close_hook(self)
  49.     #
  50.     def menu_trigger(self, menu, item):
  51.         if self.menu_hook:
  52.             self.menu_hook(self, menu, item)
  53.     #
  54.     def need_mouse(self, child): self.do_mouse = 1
  55.     def no_mouse(self, child): self.do_mouse = 0
  56.     #
  57.     def need_keybd(self, child):
  58.         self.do_keybd = 1
  59.         self.child.activate()
  60.     def no_keybd(self, child):
  61.         self.do_keybd = 0
  62.         self.child.deactivate()
  63.     #
  64.     def need_timer(self, child): self.do_timer = 1
  65.     def no_timer(self, child): self.do_timer = 0
  66.     #
  67.     def need_altdraw(self, child): self.do_altdraw = 1
  68.     def no_altdraw(self, child): self.do_altdraw = 0
  69.     #
  70.     def realize(self):
  71.         if self.win:
  72.             raise Error, 'realize(): called twice'
  73.         if not self.child:
  74.             raise Error, 'realize(): no child'
  75.         # Compute suggested size
  76.         self.size = self.child.getminsize(self.beginmeasuring(), \
  77.                           self.size)
  78.         save_defsize = stdwin.getdefwinsize()
  79.         scrwidth, scrheight = stdwin.getscrsize()
  80.         width, height = self.size
  81.         if width > scrwidth:
  82.             width = scrwidth * 2/3
  83.         if height > scrheight:
  84.             height = scrheight * 2/3
  85.         stdwin.setdefwinsize(width, height)
  86.         self.hbar, self.vbar = stdwin.getdefscrollbars()
  87.         self.win = stdwin.open(self.title)
  88.         stdwin.setdefwinsize(save_defsize)
  89.         self.win.setdocsize(self.size)
  90.         if self.itimer:
  91.             self.win.settimer(self.itimer)
  92.         width, height = self.win.getwinsize()
  93.         if self.hbar:
  94.             width = self.size[0]
  95.         if self.vbar:
  96.             height = self.size[1]
  97.         self.child.setbounds(((0, 0), (width, height)))
  98.         self.child.realize()
  99.         self.win.dispatch = self.dispatch
  100.         mainloop.register(self.win)
  101.     #
  102.     def fixup(self):
  103.         # XXX This could share code with realize() above
  104.         self.size = self.child.getminsize(self.beginmeasuring(), \
  105.                               self.win.getwinsize())
  106.         self.win.setdocsize(self.size)
  107.         width, height = self.win.getwinsize()
  108.         if self.hbar:
  109.             width = self.size[0]
  110.         if self.vbar:
  111.             height = self.size[1]
  112.         self.child.setbounds(((0, 0), (width, height)))
  113.         # Force a redraw of the entire window:
  114.         self.win.change((0, 0), self.size)
  115.     #
  116.     def beginmeasuring(self):
  117.         # Return something with which a child can measure text
  118.         if self.win:
  119.             return self.win.begindrawing()
  120.         else:
  121.             return stdwin
  122.     #
  123.     def begindrawing(self):
  124.         if self.win:
  125.             return self.win.begindrawing()
  126.         else:
  127.             raise Error, 'begindrawing(): not realized yet'
  128.     #
  129.     def getwindow(self):
  130.         if self.win:
  131.             return self.win
  132.         else:
  133.             raise Error, 'getwindow(): not realized yet'
  134.     #
  135.     def change(self, area):
  136.         if self.win:
  137.             self.win.change(area)
  138.     #
  139.     def scroll(self, area, vector):
  140.         if self.win:
  141.             self.win.scroll(area, vector)
  142.     #
  143.     def settimer(self, itimer):
  144.         if self.win:
  145.             self.win.settimer(itimer)
  146.         else:
  147.             self.itimer = itimer
  148.     #
  149.     # Only call dispatch once we are realized
  150.     #
  151.     def dispatch(self, (type, win, detail)):
  152.         if type == WE_DRAW:
  153.             d = self.win.begindrawing()
  154.             self.child.draw(d, detail)
  155.             del d
  156.             if self.do_altdraw: self.child.altdraw(detail)
  157.         elif type == WE_MOUSE_DOWN:
  158.             if self.do_mouse: self.child.mouse_down(detail)
  159.         elif type == WE_MOUSE_MOVE:
  160.             if self.do_mouse: self.child.mouse_move(detail)
  161.         elif type == WE_MOUSE_UP:
  162.             if self.do_mouse: self.child.mouse_up(detail)
  163.         elif type in (WE_CHAR, WE_COMMAND):
  164.             if self.do_keybd: self.child.keybd(type, detail)
  165.         elif type == WE_TIMER:
  166.             if self.do_timer: self.child.timer()
  167.         elif type == WE_SIZE:
  168.             self.fixup()
  169.         elif type == WE_CLOSE:
  170.             self.close_trigger()
  171.         elif type == WE_MENU:
  172.             self.menu_trigger(detail)
  173.         if self.pending_destroy:
  174.             self.destroy()
  175.     #
  176.  
  177. def MainLoop():
  178.     mainloop.mainloop()
  179.  
  180. def Dispatch(event):
  181.     mainloop.dispatch(event)
  182.  
  183. # Interface used by WindowSched:
  184.  
  185. def CountWindows():
  186.     return mainloop.countwindows()
  187.  
  188. def AnyWindow():
  189.     return mainloop.anywindow()
  190.