blob: cdec10b28314cf47d416dcc0ed8ee0cbe32c3b04 [file] [log] [blame]
Guido van Rossum2e449671990-11-05 19:44:36 +00001# 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.
Guido van Rossum117dbcb1991-04-07 13:37:05 +00004#
Guido van Rossume825f151991-08-16 13:22:23 +00005# There is also an alternative interface to "mainloop" here.
Guido van Rossum2e449671990-11-05 19:44:36 +00006
7import stdwin
8from stdwinevents import *
Guido van Rossume825f151991-08-16 13:22:23 +00009import mainloop
Guido van Rossum2e449671990-11-05 19:44:36 +000010
11from TransParent import ManageOneChild
12
13Error = 'WindowParent.Error' # Exception
14
Guido van Rossumce084481991-12-26 13:06:29 +000015class WindowParent(ManageOneChild):
Guido van Rossum2e449671990-11-05 19:44:36 +000016 #
Guido van Rossum89a78691992-12-14 12:57:56 +000017 def create(self, title, size):
Guido van Rossum2e449671990-11-05 19:44:36 +000018 self.title = title
19 self.size = size # (width, height)
Guido van Rossum52cea431991-01-23 13:43:16 +000020 self._reset()
Guido van Rossume825f151991-08-16 13:22:23 +000021 self.close_hook = WindowParent.delayed_destroy
Guido van Rossum52cea431991-01-23 13:43:16 +000022 return self
23 #
24 def _reset(self):
Guido van Rossumaa179171991-05-14 12:22:25 +000025 self.child = None
26 self.win = None
Guido van Rossum2e449671990-11-05 19:44:36 +000027 self.itimer = 0
28 self.do_mouse = 0
Guido van Rossum117dbcb1991-04-07 13:37:05 +000029 self.do_keybd = 0
Guido van Rossum2e449671990-11-05 19:44:36 +000030 self.do_timer = 0
Guido van Rossum117dbcb1991-04-07 13:37:05 +000031 self.do_altdraw = 0
Guido van Rossumfb9149c1991-04-21 19:28:44 +000032 self.pending_destroy = 0
Guido van Rossumaa179171991-05-14 12:22:25 +000033 self.close_hook = None
Guido van Rossume825f151991-08-16 13:22:23 +000034 self.menu_hook = None
Guido van Rossum52cea431991-01-23 13:43:16 +000035 #
36 def destroy(self):
Guido van Rossume825f151991-08-16 13:22:23 +000037 mainloop.unregister(self.win)
Guido van Rossum52cea431991-01-23 13:43:16 +000038 if self.child: self.child.destroy()
39 self._reset()
Guido van Rossum2e449671990-11-05 19:44:36 +000040 #
Guido van Rossumfb9149c1991-04-21 19:28:44 +000041 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 #
Guido van Rossumaa179171991-05-14 12:22:25 +000047 def close_trigger(self):
48 if self.close_hook: self.close_hook(self)
49 #
Guido van Rossum89a78691992-12-14 12:57:56 +000050 def menu_trigger(self, menu, item):
Guido van Rossume825f151991-08-16 13:22:23 +000051 if self.menu_hook:
52 self.menu_hook(self, menu, item)
53 #
Guido van Rossum2e449671990-11-05 19:44:36 +000054 def need_mouse(self, child): self.do_mouse = 1
55 def no_mouse(self, child): self.do_mouse = 0
56 #
Guido van Rossume825f151991-08-16 13:22:23 +000057 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()
Guido van Rossum117dbcb1991-04-07 13:37:05 +000063 #
Guido van Rossum2e449671990-11-05 19:44:36 +000064 def need_timer(self, child): self.do_timer = 1
65 def no_timer(self, child): self.do_timer = 0
66 #
Guido van Rossum117dbcb1991-04-07 13:37:05 +000067 def need_altdraw(self, child): self.do_altdraw = 1
68 def no_altdraw(self, child): self.do_altdraw = 0
69 #
Guido van Rossum2e449671990-11-05 19:44:36 +000070 def realize(self):
71 if self.win:
72 raise Error, 'realize(): called twice'
73 if not self.child:
74 raise Error, 'realize(): no child'
Guido van Rossume825f151991-08-16 13:22:23 +000075 # 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()
Guido van Rossum2e449671990-11-05 19:44:36 +000087 self.win = stdwin.open(self.title)
Guido van Rossume825f151991-08-16 13:22:23 +000088 stdwin.setdefwinsize(save_defsize)
Guido van Rossum117dbcb1991-04-07 13:37:05 +000089 self.win.setdocsize(self.size)
Guido van Rossum2e449671990-11-05 19:44:36 +000090 if self.itimer:
91 self.win.settimer(self.itimer)
Guido van Rossume825f151991-08-16 13:22:23 +000092 width, height = self.win.getwinsize()
93 if self.hbar:
94 width = self.size[0]
95 if self.vbar:
96 height = self.size[1]
Guido van Rossum89a78691992-12-14 12:57:56 +000097 self.child.setbounds(((0, 0), (width, height)))
Guido van Rossum117dbcb1991-04-07 13:37:05 +000098 self.child.realize()
99 self.win.dispatch = self.dispatch
Guido van Rossume825f151991-08-16 13:22:23 +0000100 mainloop.register(self.win)
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000101 #
102 def fixup(self):
Guido van Rossume825f151991-08-16 13:22:23 +0000103 # XXX This could share code with realize() above
104 self.size = self.child.getminsize(self.beginmeasuring(), \
105 self.win.getwinsize())
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000106 self.win.setdocsize(self.size)
Guido van Rossume825f151991-08-16 13:22:23 +0000107 width, height = self.win.getwinsize()
108 if self.hbar:
109 width = self.size[0]
110 if self.vbar:
111 height = self.size[1]
Guido van Rossum89a78691992-12-14 12:57:56 +0000112 self.child.setbounds(((0, 0), (width, height)))
Guido van Rossumaa179171991-05-14 12:22:25 +0000113 # Force a redraw of the entire window:
Guido van Rossume825f151991-08-16 13:22:23 +0000114 self.win.change((0, 0), self.size)
Guido van Rossum2e449671990-11-05 19:44:36 +0000115 #
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 #
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000129 def getwindow(self):
130 if self.win:
131 return self.win
132 else:
133 raise Error, 'getwindow(): not realized yet'
134 #
Guido van Rossum2e449671990-11-05 19:44:36 +0000135 def change(self, area):
136 if self.win:
137 self.win.change(area)
138 #
Guido van Rossumfea2af11993-01-04 09:16:51 +0000139 def scroll(self, area, vector):
Guido van Rossum2e449671990-11-05 19:44:36 +0000140 if self.win:
Guido van Rossumfea2af11993-01-04 09:16:51 +0000141 self.win.scroll(area, vector)
Guido van Rossum2e449671990-11-05 19:44:36 +0000142 #
143 def settimer(self, itimer):
144 if self.win:
145 self.win.settimer(itimer)
146 else:
147 self.itimer = itimer
148 #
Guido van Rossumfb9149c1991-04-21 19:28:44 +0000149 # Only call dispatch once we are realized
Guido van Rossum2e449671990-11-05 19:44:36 +0000150 #
151 def dispatch(self, (type, win, detail)):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000152 if type == WE_DRAW:
Guido van Rossum2e449671990-11-05 19:44:36 +0000153 d = self.win.begindrawing()
154 self.child.draw(d, detail)
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000155 del d
156 if self.do_altdraw: self.child.altdraw(detail)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000157 elif type == WE_MOUSE_DOWN:
Guido van Rossum2e449671990-11-05 19:44:36 +0000158 if self.do_mouse: self.child.mouse_down(detail)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000159 elif type == WE_MOUSE_MOVE:
Guido van Rossum2e449671990-11-05 19:44:36 +0000160 if self.do_mouse: self.child.mouse_move(detail)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000161 elif type == WE_MOUSE_UP:
Guido van Rossum2e449671990-11-05 19:44:36 +0000162 if self.do_mouse: self.child.mouse_up(detail)
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000163 elif type in (WE_CHAR, WE_COMMAND):
164 if self.do_keybd: self.child.keybd(type, detail)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000165 elif type == WE_TIMER:
Guido van Rossum2e449671990-11-05 19:44:36 +0000166 if self.do_timer: self.child.timer()
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000167 elif type == WE_SIZE:
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000168 self.fixup()
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000169 elif type == WE_CLOSE:
Guido van Rossumaa179171991-05-14 12:22:25 +0000170 self.close_trigger()
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000171 elif type == WE_MENU:
Guido van Rossume825f151991-08-16 13:22:23 +0000172 self.menu_trigger(detail)
Guido van Rossumfb9149c1991-04-21 19:28:44 +0000173 if self.pending_destroy:
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000174 self.destroy()
Guido van Rossum2e449671990-11-05 19:44:36 +0000175 #
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000176
177def MainLoop():
Guido van Rossume825f151991-08-16 13:22:23 +0000178 mainloop.mainloop()
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000179
180def Dispatch(event):
Guido van Rossume825f151991-08-16 13:22:23 +0000181 mainloop.dispatch(event)
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000182
183# Interface used by WindowSched:
184
185def CountWindows():
Guido van Rossume825f151991-08-16 13:22:23 +0000186 return mainloop.countwindows()
Guido van Rossum117dbcb1991-04-07 13:37:05 +0000187
188def AnyWindow():
Guido van Rossume825f151991-08-16 13:22:23 +0000189 return mainloop.anywindow()