6 from Carbon
import Events
15 if not hasattr(sys
, 'ps1'):
17 if not hasattr(sys
, 'ps2'):
20 def inspect(foo
): # JJS 1/25/99
21 "Launch the browser on the given object. This is a general built-in function."
23 PyBrowser
.Browser(foo
)
25 class ConsoleTextWidget(W
.EditText
):
27 def __init__(self
, *args
, **kwargs
):
28 apply(W
.EditText
.__init
__, (self
,) + args
, kwargs
)
31 self
.pyinteractive
= PyInteractive
.PyInteractive()
34 self
._namespace
= __main__
.__dict
__
35 self
._namespace
['inspect'] = inspect
# JJS 1/25/99
37 def insert(self
, text
):
39 self
.ted
.WEInsert(text
, None, None)
43 def set_namespace(self
, dict):
44 if type(dict) <> DictionaryType
:
45 raise TypeError, "The namespace needs to be a dictionary"
46 if 'inspect' not in dict.keys(): dict['inspect'] = inspect
# JJS 1/25/99
47 self
._namespace
= dict
52 self
.write('Python %s\n' % sys
.version
)
53 self
.write('Type "copyright", "credits" or "license" for more information.\n')
54 self
.write('MacPython IDE %s\n' % __main__
.__version
__)
58 def key(self
, char
, event
):
59 (what
, message
, when
, where
, modifiers
) = event
60 if self
._enabled
and not modifiers
& Events
.cmdKey
or char
in Wkeys
.arrowkeys
:
61 if char
not in Wkeys
.navigationkeys
:
63 if char
== Wkeys
.enterkey
:
64 char
= Wkeys
.returnkey
65 selstart
, selend
= self
.getselection()
66 if char
== Wkeys
.backspacekey
:
67 if selstart
<= (self
._inputstart
- (selstart
<> selend
)):
69 self
.ted
.WEKey(ord(char
), modifiers
)
70 if char
not in Wkeys
.navigationkeys
:
72 if char
not in Wkeys
.scrollkeys
:
74 self
.updatescrollbars()
75 if char
== Wkeys
.returnkey
:
76 text
= self
.get()[self
._inputstart
:selstart
]
77 text
= string
.join(string
.split(text
, "\r"), "\n")
78 saveyield
= MacOS
.EnableAppswitch(0)
79 self
.pyinteractive
.executeline(text
, self
, self
._namespace
)
80 MacOS
.EnableAppswitch(saveyield
)
81 selstart
, selend
= self
.getselection()
82 self
._inputstart
= selstart
84 def domenu_save_as(self
, *args
):
86 fss
, ok
= macfs
.StandardPutFile('Save console text as:', 'console.txt')
89 f
= open(fss
.as_pathname(), 'wb')
92 fss
.SetCreatorType(W
._signature
, 'TEXT')
94 def write(self
, text
):
95 self
._buf
= self
._buf
+ text
100 stuff
= string
.split(self
._buf
, '\n')
101 stuff
= string
.join(stuff
, '\r')
102 self
.setselection_at_end()
103 self
.ted
.WEInsert(stuff
, None, None)
104 selstart
, selend
= self
.getselection()
105 self
._inputstart
= selstart
107 self
.ted
.WEClearUndo()
108 self
.updatescrollbars()
110 def selection_ok(self
):
111 selstart
, selend
= self
.getselection()
112 return not (selstart
< self
._inputstart
or selend
< self
._inputstart
)
114 def checkselection(self
):
115 if not self
.selection_ok():
116 self
.setselection_at_end()
118 def setselection_at_end(self
):
119 end
= self
.ted
.WEGetTextLength()
120 self
.setselection(end
, end
)
121 self
.updatescrollbars()
123 def domenu_cut(self
, *args
):
124 if not self
.selection_ok():
126 W
.EditText
.domenu_cut(self
)
128 def domenu_paste(self
, *args
):
129 if not self
.selection_ok():
130 self
.setselection_at_end()
131 W
.EditText
.domenu_paste(self
)
133 def domenu_clear(self
, *args
):
134 if not self
.selection_ok():
136 W
.EditText
.domenu_clear(self
)
139 class PyConsole(W
.Window
):
141 def __init__(self
, bounds
, show
= 1, fontsettings
= ("Monaco", 0, 9, (0, 0, 0)),
142 tabsettings
= (32, 0), unclosable
= 0):
143 W
.Window
.__init
__(self
,
145 "Python Interactive",
146 minsize
= (200, 100),
150 self
._unclosable
= unclosable
151 consoletext
= ConsoleTextWidget((-1, -1, -14, 1), inset
= (6, 5),
152 fontsettings
= fontsettings
, tabsettings
= tabsettings
)
153 self
._bary
= W
.Scrollbar((-15, 14, 16, -14), consoletext
.vscroll
, max = 32767)
154 self
.consoletext
= consoletext
155 self
.namespacemenu
= W
.PopupMenu((-15, -1, 16, 16), [], self
.consoletext
.set_namespace
)
156 self
.namespacemenu
.bind('<click>', self
.makenamespacemenu
)
159 def makenamespacemenu(self
, *args
):
161 namespacelist
= self
.getnamespacelist()
162 self
.namespacemenu
.set([("Clear window", self
.clearbuffer
), ("Font settings\xc9", self
.dofontsettings
),
163 ["Namespace"] + namespacelist
, ("Browse namespace\xc9", self
.browsenamespace
)])
164 currentname
= self
.consoletext
._namespace
["__name__"]
165 for i
in range(len(namespacelist
)):
166 if namespacelist
[i
][0] == currentname
:
170 # XXX this functionality should be generally available in Wmenus
171 submenuid
= self
.namespacemenu
.menu
.menu
.GetItemMark(3)
172 menu
= self
.namespacemenu
.menu
.bar
.menus
[submenuid
]
173 menu
.menu
.CheckMenuItem(i
+ 1, 1)
175 def browsenamespace(self
):
178 PyBrowser
.Browser(self
.consoletext
._namespace
, self
.consoletext
._namespace
["__name__"])
180 def clearbuffer(self
):
181 from Carbon
import Res
182 self
.consoletext
.ted
.WEUseText(Res
.Resource(''))
183 self
.consoletext
.write(sys
.ps1
)
184 self
.consoletext
.flush()
186 def getnamespacelist(self
):
189 editors
= filter(lambda x
: x
.__class
__.__name
__ == "Editor", self
.parent
._windows
.values())
191 namespaces
= [ ("__main__",__main__
.__dict
__) ]
193 modname
= os
.path
.splitext(ed
.title
)[0]
194 if sys
.modules
.has_key(modname
):
195 module
= sys
.modules
[modname
]
196 namespaces
.append((modname
, module
.__dict
__))
198 if ed
.title
[-3:] == '.py':
199 modname
= ed
.title
[:-3]
202 ed
.globals["__name__"] = modname
203 namespaces
.append((modname
, ed
.globals))
206 def dofontsettings(self
):
208 settings
= FontSettings
.FontDialog(self
.consoletext
.getfontsettings(),
209 self
.consoletext
.gettabsettings())
211 fontsettings
, tabsettings
= settings
212 self
.consoletext
.setfontsettings(fontsettings
)
213 self
.consoletext
.settabsettings(tabsettings
)
215 def show(self
, onoff
= 1):
216 W
.Window
.show(self
, onoff
)
226 def writeprefs(self
):
227 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
228 prefs
.console
.show
= self
.isvisible()
229 prefs
.console
.windowbounds
= self
.getbounds()
230 prefs
.console
.fontsettings
= self
.consoletext
.getfontsettings()
231 prefs
.console
.tabsettings
= self
.consoletext
.gettabsettings()
235 class OutputTextWidget(W
.EditText
):
237 def domenu_save_as(self
, *args
):
238 title
= self
._parentwindow
.gettitle()
240 fss
, ok
= macfs
.StandardPutFile('Save %s text as:' % title
, title
+ '.txt')
243 f
= open(fss
.as_pathname(), 'wb')
246 fss
.SetCreatorType(W
._signature
, 'TEXT')
251 def __init__(self
, bounds
, show
= 1, fontsettings
= ("Monaco", 0, 9, (0, 0, 0)), tabsettings
= (32, 0)):
253 self
.fontsettings
= fontsettings
254 self
.tabsettings
= tabsettings
258 # should be able to set this
259 self
.savestdout
, self
.savestderr
= sys
.stdout
, sys
.stderr
260 sys
.stderr
= sys
.stdout
= self
264 def setupwidgets(self
):
265 self
.w
= W
.Window(self
.bounds
, "Output",
266 minsize
= (200, 100),
268 self
.w
.outputtext
= OutputTextWidget((-1, -1, -14, 1), inset
= (6, 5),
269 fontsettings
= self
.fontsettings
, tabsettings
= self
.tabsettings
, readonly
= 1)
270 menuitems
= [("Clear window", self
.clearbuffer
), ("Font settings\xc9", self
.dofontsettings
)]
271 self
.w
.popupmenu
= W
.PopupMenu((-15, -1, 16, 16), menuitems
)
273 self
.w
._bary
= W
.Scrollbar((-15, 14, 16, -14), self
.w
.outputtext
.vscroll
, max = 32767)
274 self
.w
.bind("<close>", self
.close
)
275 self
.w
.bind("<activate>", self
.activate
)
277 def write(self
, text
):
278 oldyield
= MacOS
.EnableAppswitch(-1)
280 self
._buf
= self
._buf
+ text
281 if '\n' in self
._buf
:
284 MacOS
.EnableAppswitch(oldyield
)
288 stuff
= string
.split(self
._buf
, '\n')
289 stuff
= string
.join(stuff
, '\r')
290 end
= self
.w
.outputtext
.ted
.WEGetTextLength()
291 self
.w
.outputtext
.setselection(end
, end
)
292 self
.w
.outputtext
.ted
.WEFeatureFlag(WASTEconst
.weFReadOnly
, 0)
293 self
.w
.outputtext
.ted
.WEInsert(stuff
, None, None)
295 self
.w
.outputtext
.updatescrollbars()
296 self
.w
.outputtext
.ted
.WEFeatureFlag(WASTEconst
.weFReadOnly
, 1)
303 self
.w
.outputtext
.updatescrollbars()
310 def writeprefs(self
):
311 if self
.w
is not None:
312 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
313 prefs
.output
.show
= self
.w
.isvisible()
314 prefs
.output
.windowbounds
= self
.w
.getbounds()
315 prefs
.output
.fontsettings
= self
.w
.outputtext
.getfontsettings()
316 prefs
.output
.tabsettings
= self
.w
.outputtext
.gettabsettings()
319 def dofontsettings(self
):
321 settings
= FontSettings
.FontDialog(self
.w
.outputtext
.getfontsettings(),
322 self
.w
.outputtext
.gettabsettings())
324 fontsettings
, tabsettings
= settings
325 self
.w
.outputtext
.setfontsettings(fontsettings
)
326 self
.w
.outputtext
.settabsettings(tabsettings
)
328 def clearbuffer(self
):
329 from Carbon
import Res
330 self
.w
.outputtext
.set('')
332 def activate(self
, onoff
):
346 # A trick to make the input dialog box a bit more palatable
347 if hasattr(sys
.stdout
, '_buf'):
348 prompt
= sys
.stdout
._buf
352 prompt
= "Stdin input:"
354 rv
= EasyDialogs
.AskString(prompt
)
360 def installconsole(defaultshow
= 1):
362 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
363 if not prefs
.console
or not hasattr(prefs
.console
, 'show'):
364 prefs
.console
.show
= defaultshow
365 if not hasattr(prefs
.console
, "windowbounds"):
366 prefs
.console
.windowbounds
= (450, 250)
367 if not hasattr(prefs
.console
, "fontsettings"):
368 prefs
.console
.fontsettings
= ("Monaco", 0, 9, (0, 0, 0))
369 if not hasattr(prefs
.console
, "tabsettings"):
370 prefs
.console
.tabsettings
= (32, 0)
371 console
= PyConsole(prefs
.console
.windowbounds
, prefs
.console
.show
,
372 prefs
.console
.fontsettings
, prefs
.console
.tabsettings
, 1)
374 def installoutput(defaultshow
= 0, OutPutWindow
= PyOutput
):
377 # quick 'n' dirty std in emulation
378 sys
.stdin
= SimpleStdin()
380 prefs
= MacPrefs
.GetPrefs(W
.getapplication().preffilepath
)
381 if not prefs
.output
or not hasattr(prefs
.output
, 'show'):
382 prefs
.output
.show
= defaultshow
383 if not hasattr(prefs
.output
, "windowbounds"):
384 prefs
.output
.windowbounds
= (450, 250)
385 if not hasattr(prefs
.output
, "fontsettings"):
386 prefs
.output
.fontsettings
= ("Monaco", 0, 9, (0, 0, 0))
387 if not hasattr(prefs
.output
, "tabsettings"):
388 prefs
.output
.tabsettings
= (32, 0)
389 output
= OutPutWindow(prefs
.output
.windowbounds
, prefs
.output
.show
,
390 prefs
.output
.fontsettings
, prefs
.output
.tabsettings
)