/library/Window.py

http://pyjamas.googlecode.com/ · Python · 145 lines · 109 code · 30 blank · 6 comment · 4 complexity · 1ad78391dafad45e4461b6c4d306056b MD5 · raw file

  1. import Location
  2. closingListeners = []
  3. resizeListeners = []
  4. def addWindowCloseListener(listener):
  5. global closingListeners
  6. closingListeners.append(listener)
  7. def addWindowResizeListener(listener):
  8. global resizeListeners
  9. resizeListeners.append(listener)
  10. def alert(msg):
  11. JS("""
  12. $wnd.alert(msg);
  13. """)
  14. def confirm(msg):
  15. JS("""
  16. return $wnd.confirm(msg);
  17. """)
  18. def enableScrolling(enable):
  19. JS("""
  20. $doc.body.style.overflow = enable ? 'auto' : 'hidden';
  21. """)
  22. def getClientHeight():
  23. JS("""
  24. if ($wnd.innerHeight)
  25. return $wnd.innerHeight;
  26. return $doc.body.clientHeight;
  27. """)
  28. def getClientWidth():
  29. JS("""
  30. if ($wnd.innerWidth)
  31. return $wnd.innerWidth;
  32. return $doc.body.clientWidth;
  33. """)
  34. JS("var Window_location = null")
  35. def getLocation():
  36. global location
  37. JS("""
  38. if(!Window_location)
  39. Window_location = Location_Location($wnd.location);
  40. return Window_location;
  41. """)
  42. def getTitle():
  43. JS("""
  44. return $doc.title;
  45. """)
  46. def open(url, name, features):
  47. JS("""
  48. $wnd.open(url, name, features);
  49. """)
  50. def removeWindowCloseListener(listener):
  51. global closingListeners
  52. closingListeners.remove(listener)
  53. def removeWindowResizeListener(listener):
  54. global resizeListeners
  55. resizeListeners.remove(listener)
  56. def setMargin(size):
  57. JS("""
  58. $doc.body.style.margin = size;
  59. """)
  60. def setTitle(title):
  61. JS("""
  62. $doc.title = title;
  63. """)
  64. # TODO: call fireClosedAndCatch
  65. def onClosed():
  66. fireClosedImpl()
  67. # TODO: call fireClosingAndCatch
  68. def onClosing():
  69. fireClosingImpl()
  70. # TODO: call fireResizedAndCatch
  71. def onResize():
  72. fireResizedImpl()
  73. def fireClosedAndCatch(handler):
  74. # FIXME - need implementation
  75. pass
  76. def fireClosedImpl():
  77. global closingListeners
  78. for listener in closingListeners:
  79. listener.onWindowClosed()
  80. def fireClosingAndCatch(handler):
  81. # FIXME - need implementation
  82. pass
  83. def fireClosingImpl():
  84. global closingListeners
  85. ret = None
  86. for listener in closingListeners:
  87. msg = listener.onWindowClosing()
  88. if ret == None:
  89. ret = msg
  90. return ret
  91. def fireResizedAndCatch(handler):
  92. # FIXME - need implementation
  93. pass
  94. def fireResizedImpl():
  95. global resizeListeners
  96. for listener in resizeListeners:
  97. listener.onWindowResized(getClientWidth(), getClientHeight())
  98. def init():
  99. JS("""
  100. $wnd.__pygwt_initHandlers(
  101. function() {
  102. Window_onResize();
  103. },
  104. function() {
  105. return Window_onClosing();
  106. },
  107. function() {
  108. Window_onClosed();
  109. $wnd.onresize = null;
  110. $wnd.onbeforeclose = null;
  111. $wnd.onclose = null;
  112. }
  113. );
  114. """)
  115. init()