6 from Carbon
import Events
16 if not hasattr(sys
, 'ps1'):
18 if not hasattr(sys
, 'ps2'):
21 def inspect(foo
): # JJS 1/25/99
22 "Launch the browser on the given object. This is a general built-in function."
24 PyBrowser
.Browser(foo
)
26 class ConsoleTextWidget(W
.EditText
):
28 def __init__(self
, *args
, **kwargs
):
29 apply(W
.EditText
.__init
__, (self
,) + args
, kwargs
)
32 self
.pyinteractive
= PyInteractive
.PyInteractive()
35 self
._namespace
= __main__
.__dict
__
36 self
._namespace
['inspect'] = inspect
# JJS 1/25/99
38 def insert(self
, text
):
40 self
.ted
.WEInsert(text
, None, None)
44 def set_namespace(self
, dict):
45 if type(dict) <> DictionaryType
:
46 raise TypeError, "The namespace needs to be a dictionary"
47 if 'inspect' not in dict.keys(): dict['inspect'] = inspect
# JJS 1/25/99
48 self
._namespace
= dict
53 self
.write('Python %s\n' % sys
.version
)
54 self
.write('Type "copyright", "credits" or "license" for more information.\n')
55 self
.write('MacPython IDE %s\n' % __main__
.__version
__)
59 def key(self
, char
, event
):
60 (what
, message
, when
, where
, modifiers
) = event
61 if self
._enabled
and not modifiers
& Events
.cmdKey
or char
in Wkeys
.arrowkeys
:
62 if char
not in Wkeys
.navigationkeys
:
64 if char
== Wkeys
.enterkey
:
65 char
= Wkeys
.returnkey
66 selstart
, selend
= self
.getselection()
67 if char
== Wkeys
.backspacekey
:
68 if selstart
<= (self
._inputstart
- (selstart
<> selend
)):
70 self
.ted
.WEKey(ord(char
), modifiers
)
71 if char
not in Wkeys
.navigationkeys
:
73 if char
not in Wkeys
.scrollkeys
:
75 self
.updatescrollbars()
76 if char
== Wkeys
.returnkey
:
77 text
= self
.get()[self
._inputstart
:selstart
]
78 text
= string
.join(string
.split(text
, "\r"), "\n")
79 if hasattr(MacOS
, 'EnableAppswitch'):
80 saveyield
= MacOS
.EnableAppswitch(0)
81 self
.pyinteractive
.executeline(text
, self
, self
._namespace
)
82 if hasattr(MacOS
, 'EnableAppswitch'):
83 MacOS
.EnableAppswitch(saveyield
)
84 selstart
, selend
= self
.getselection()
85 self
._inputstart
= selstart
87 def domenu_save_as(self
, *args
):
89 filename
= EasyDialogs
.AskFileForSave(message
='Save console text as:',
90 savedFileName
='console.txt')
93 f
= open(filename
, 'wb')
96 fss
.SetCreatorType(W
._signature
, 'TEXT')
98 def write(self
, text
):
99 self
._buf
= self
._buf
+ text
100 if '\n' in self
._buf
:
104 stuff
= string
.split(self
._buf
, '\n')
105 stuff
= string
.join(stuff
, '\r')
106 self
.setselection_at_end()
107 self
.ted
.WEInsert(stuff
, None, None)
108 selstart
, selend
= self
.getselection()
109 self
._inputstart
= selstart
111 self
.ted
.WEClearUndo()
112 self
.updatescrollbars()
113 if self
._parentwindow
.wid
.GetWindowPort().QDIsPortBuffered():
114 self
._parentwindow
.wid
.GetWindowPort().QDFlushPortBuffer(None)
116 def selection_ok(self
):
117 selstart
, selend
= self
.getselection()
118 return not (selstart
< self
._inputstart
or selend
< self
._inputstart
)
120 def checkselection(self
):
121 if not self
.selection_ok():
122 self
.setselection_at_end()
124 def setselection_at_end(self
):
125 end
= self
.ted
.WEGetTextLength()
126 self
.setselection(end
, end
)
127 self
.updatescrollbars()
129 def domenu_cut(self
, *args
):
130 if not self
.selection_ok():
132 W
.EditText
.domenu_cut(self
)
134 def domenu_paste(self
, *args
):
135 if not self
.selection_ok():
136 self
.setselection_at_end()
137 W
.EditText
.domenu_paste(self
)
139 def domenu_clear(self
, *args
):
140 if not self
.selection_ok():
142 W
.EditText
.domenu_clear(self
)
145 class PyConsole(W
.Window
):
147 def __init__(self
, bounds
, show
= 1, fontsettings
= ("Monaco", 0, 9, (0, 0, 0)),
148 tabsettings
= (32, 0), unclosable
= 0):
149 W
.Window
.__init
__(self
,
151 "Python Interactive",
152 minsize
= (200, 100),
156 self
._unclosable
= unclosable
157 consoletext
= ConsoleTextWidget((-1, -1, -14, 1), inset
= (6, 5),
158 fontsettings
= fontsettings
, tabsettings
= tabsettings
)
159 self
._bary
= W
.Scrollbar((-15, 14, 16, -14), consoletext
.vscroll
, max = 32767)
160 self
.consoletext
= consoletext
161 self
.namespacemenu
= W
.PopupMenu((-15, -1, 16, 16), [], self
.consoletext
.set_namespace
)
162 self
.namespacemenu
.bind('<click>', self
.makenamespacemenu
)
165 def makenamespacemenu(self
, *args
):
167 namespacelist
= self
.getnamespacelist()
168 self
.namespacemenu
.set([("Clear window", self
.clearbuffer
), ("Font settings\xc9", self
.dofontsettings
),
169 ["Namespace"] + namespacelist
, ("Browse namespace\xc9", self
.browsenamespace
)])
170 currentname
= self
.consoletext
._namespace
["__name__"]
171 for i
in range(len(namespacelist
)):
172 if namespacelist
[i
][0] == currentname
:
176 # XXX this functionality should be generally available in Wmenus
177 submenuid
= self
.namespacemenu
.menu
.menu
.GetItemMark(3)
178 menu
= self
.namespacemenu
.menu
.bar
.menus
[submenuid
]
179 menu
.menu
.CheckMenuItem(i
+ 1, 1)
181 def browsenamespace(self
):
184 PyBrowser
.Browser(self
.consoletext
._namespace
, self
.consoletext
._namespace
["__name__"])
186 def clearbuffer(self
):
187 from Carbon
import Res
188 self
.consoletext
.ted
.WEUseText(Res
.Resource(''))
189 self
.consoletext
.write(sys
.ps1
)
190 self
.consoletext
.flush()
192 def getnamespacelist(self
):
195 editors
= filter(lambda x
: x
.__class
__.__name
__ == "Editor", self
.parent
._windows
.values())
197 namespaces
= [ ("__main__",__main__
.__dict
__) ]
199 modname
= os
.path
.splitext(ed
.title
)[0]
200 if sys
.modules
.has_key(modname
):
201 module
= sys
.modules
[modname
]
202 namespaces
.append((modname
, module
.__dict
__))
204 if ed
.title
[-3:] == '.py':
205 modname
= ed
.title
[:-3]
208 ed
.globals["__name__"] = modname
209 namespaces
.append((modname
, ed
.globals))
212 def dofontsettings(self
):
214 settings
= FontSettings
.FontDialog(self
.consoletext
.getfontsettings(),
215 self
.consoletext
.gettabsettings())
217 fontsettings
, tabsettings
= settings
218 self
.consoletext
.setfontsettings(fontsettings
)
219 self
.consoletext
.settabsettings(tabsettings
)
221 def show(self
, onoff
= 1):
222 W
.Window
.show(self
, onoff
)
232 def writeprefs(self
):
233 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
234 prefs
.console
.show
= self
.isvisible()
235 prefs
.console
.windowbounds
= self
.getbounds()
236 prefs
.console
.fontsettings
= self
.consoletext
.getfontsettings()
237 prefs
.console
.tabsettings
= self
.consoletext
.gettabsettings()
241 class OutputTextWidget(W
.EditText
):
243 def domenu_save_as(self
, *args
):
244 title
= self
._parentwindow
.gettitle()
246 filename
= EasyDialogs
.AskFileForSave(message
='Save %s text as:' % title
,
247 savedFileName
=title
+ '.txt')
250 f
= open(filename
, 'wb')
253 fss
.SetCreatorType(W
._signature
, 'TEXT')
255 def domenu_cut(self
, *args
):
256 self
.domenu_copy(*args
)
258 def domenu_clear(self
, *args
):
264 def __init__(self
, bounds
, show
= 1, fontsettings
= ("Monaco", 0, 9, (0, 0, 0)), tabsettings
= (32, 0)):
266 self
.fontsettings
= fontsettings
267 self
.tabsettings
= tabsettings
271 # should be able to set this
272 self
.savestdout
, self
.savestderr
= sys
.stdout
, sys
.stderr
273 sys
.stderr
= sys
.stdout
= self
277 def setupwidgets(self
):
278 self
.w
= W
.Window(self
.bounds
, "Output",
279 minsize
= (200, 100),
281 self
.w
.outputtext
= OutputTextWidget((-1, -1, -14, 1), inset
= (6, 5),
282 fontsettings
= self
.fontsettings
, tabsettings
= self
.tabsettings
, readonly
= 1)
283 menuitems
= [("Clear window", self
.clearbuffer
), ("Font settings\xc9", self
.dofontsettings
)]
284 self
.w
.popupmenu
= W
.PopupMenu((-15, -1, 16, 16), menuitems
)
286 self
.w
._bary
= W
.Scrollbar((-15, 14, 16, -14), self
.w
.outputtext
.vscroll
, max = 32767)
287 self
.w
.bind("<close>", self
.close
)
288 self
.w
.bind("<activate>", self
.activate
)
290 def write(self
, text
):
291 if hasattr(MacOS
, 'EnableAppswitch'):
292 oldyield
= MacOS
.EnableAppswitch(-1)
294 self
._buf
= self
._buf
+ text
295 if '\n' in self
._buf
:
298 if hasattr(MacOS
, 'EnableAppswitch'):
299 MacOS
.EnableAppswitch(oldyield
)
303 stuff
= string
.split(self
._buf
, '\n')
304 stuff
= string
.join(stuff
, '\r')
305 end
= self
.w
.outputtext
.ted
.WEGetTextLength()
306 self
.w
.outputtext
.setselection(end
, end
)
307 self
.w
.outputtext
.ted
.WEFeatureFlag(WASTEconst
.weFReadOnly
, 0)
308 self
.w
.outputtext
.ted
.WEInsert(stuff
, None, None)
310 self
.w
.outputtext
.updatescrollbars()
311 self
.w
.outputtext
.ted
.WEFeatureFlag(WASTEconst
.weFReadOnly
, 1)
312 if self
.w
.wid
.GetWindowPort().QDIsPortBuffered():
313 self
.w
.wid
.GetWindowPort().QDFlushPortBuffer(None)
320 self
.w
.outputtext
.updatescrollbars()
327 def writeprefs(self
):
328 if self
.w
is not None:
329 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
330 prefs
.output
.show
= self
.w
.isvisible()
331 prefs
.output
.windowbounds
= self
.w
.getbounds()
332 prefs
.output
.fontsettings
= self
.w
.outputtext
.getfontsettings()
333 prefs
.output
.tabsettings
= self
.w
.outputtext
.gettabsettings()
336 def dofontsettings(self
):
338 settings
= FontSettings
.FontDialog(self
.w
.outputtext
.getfontsettings(),
339 self
.w
.outputtext
.gettabsettings())
341 fontsettings
, tabsettings
= settings
342 self
.w
.outputtext
.setfontsettings(fontsettings
)
343 self
.w
.outputtext
.settabsettings(tabsettings
)
345 def clearbuffer(self
):
346 from Carbon
import Res
347 self
.w
.outputtext
.set('')
349 def activate(self
, onoff
):
363 # A trick to make the input dialog box a bit more palatable
364 if hasattr(sys
.stdout
, '_buf'):
365 prompt
= sys
.stdout
._buf
369 prompt
= "Stdin input:"
371 rv
= EasyDialogs
.AskString(prompt
)
374 rv
= rv
+ "\n" # readline should include line terminator
375 sys
.stdout
.write(rv
) # echo user's reply
379 def installconsole(defaultshow
= 1):
381 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
382 if not prefs
.console
or not hasattr(prefs
.console
, 'show'):
383 prefs
.console
.show
= defaultshow
384 if not hasattr(prefs
.console
, "windowbounds"):
385 prefs
.console
.windowbounds
= (450, 250)
386 if not hasattr(prefs
.console
, "fontsettings"):
387 prefs
.console
.fontsettings
= ("Monaco", 0, 9, (0, 0, 0))
388 if not hasattr(prefs
.console
, "tabsettings"):
389 prefs
.console
.tabsettings
= (32, 0)
390 console
= PyConsole(prefs
.console
.windowbounds
, prefs
.console
.show
,
391 prefs
.console
.fontsettings
, prefs
.console
.tabsettings
, 1)
393 def installoutput(defaultshow
= 0, OutPutWindow
= PyOutput
):
396 # quick 'n' dirty std in emulation
397 sys
.stdin
= SimpleStdin()
399 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
400 if not prefs
.output
or not hasattr(prefs
.output
, 'show'):
401 prefs
.output
.show
= defaultshow
402 if not hasattr(prefs
.output
, "windowbounds"):
403 prefs
.output
.windowbounds
= (450, 250)
404 if not hasattr(prefs
.output
, "fontsettings"):
405 prefs
.output
.fontsettings
= ("Monaco", 0, 9, (0, 0, 0))
406 if not hasattr(prefs
.output
, "tabsettings"):
407 prefs
.output
.tabsettings
= (32, 0)
408 output
= OutPutWindow(prefs
.output
.windowbounds
, prefs
.output
.show
,
409 prefs
.output
.fontsettings
, prefs
.output
.tabsettings
)