Commit 301a49b7 authored by Manish Singh's avatar Manish Singh Committed by Manish Singh

plug-ins/pygimp/plug-ins/gtkcons.py Use newer gtkcons widget from pygtk.

2005-02-19  Manish Singh  <yosh@gimp.org>

        * plug-ins/pygimp/plug-ins/gtkcons.py
        * plug-ins/pygimp/plug-ins/gimpcons.py: Use newer gtkcons widget
        from pygtk. Some cosmetic additions.
parent caaf65cc
2005-02-19 Manish Singh <yosh@gimp.org>
* plug-ins/pygimp/plug-ins/gtkcons.py
* plug-ins/pygimp/plug-ins/gimpcons.py: Use newer gtkcons widget
from pygtk. Some cosmetic additions.
2005-02-19 Manish Singh <yosh@gimp.org>
* plug-ins/pygimp/plug-ins/py-slice.py: Ignore guides at or beyond
......
......@@ -23,7 +23,7 @@ def plug_in_python_fu_console():
import pygtk
pygtk.require('2.0')
import gtk, gimpenums, gimpshelf
import gobject, gtk, gimpenums, gimpshelf
gtk.rc_parse(gimp.gtkrc())
......@@ -39,58 +39,58 @@ def plug_in_python_fu_console():
def bye(*args):
gtk.main_quit()
win = gtk.Window()
win.connect("destroy", bye)
win.set_title("Gimp-Python Console")
dialog = gtk.Dialog(title="Python Console",
buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
dialog.set_has_separator(False)
dialog.connect("response", bye)
import gtkcons
cons = gtkcons.Console(namespace=namespace, quit_cb=bye)
cons = gtkcons.Console(namespace=namespace, quit_cb=bye, closer=False)
def browse(button, cons):
import gimpprocbrowser
def on_apply(proc):
cmd = ''
def on_apply(proc):
cmd = ''
if len(proc.return_vals) > 0:
cmd = ', '.join([x[1] for x in proc.return_vals]) + ' = '
if len(proc.return_vals) > 0:
cmd = ', '.join([x[1] for x in proc.return_vals]) + ' = '
if '-' in proc.proc_name:
cmd = cmd + "pdb['%s']" % proc.proc_name
else:
cmd = cmd + "pdb.%s" % proc.proc_name
if '-' in proc.proc_name:
cmd = cmd + "pdb['%s']" % proc.proc_name
else:
cmd = cmd + "pdb.%s" % proc.proc_name
if len(proc.params) > 0 and proc.params[0][1] == 'run_mode':
params = proc.params[1:]
else:
params = proc.params
if len(proc.params) > 0 and proc.params[0][1] == 'run_mode':
params = proc.params[1:]
else:
params = proc.params
cmd = cmd + "(%s)" % ', '.join([x[1] for x in params])
cmd = cmd + "(%s)" % ', '.join([x[1] for x in params])
cons.line.set_text(cmd)
cons.line.set_text(cmd)
dlg = gimpprocbrowser.dialog_new(on_apply)
def browse(button, cons):
import gimpprocbrowser
gimpprocbrowser.dialog_new(on_apply)
button = gtk.Button("Browse")
button = gtk.Button("_Browse...")
button.connect("clicked", browse, cons)
cons.inputbox.pack_end(button, expand=FALSE)
cons.inputbox.pack_end(button, fill=False, expand=False, padding=2)
button.show()
win.add(cons)
dialog.vbox.pack_start(cons)
cons.show()
win.set_default_size(475, 300)
win.show()
dialog.set_default_size(500, 500)
dialog.show()
cons.init()
# flush the displays every half second
def timeout():
gimp.displays_flush()
return TRUE
return True
gtk.timeout_add(500, timeout)
gobject.timeout_add(500, timeout)
gtk.main()
register(
......
#!/usr/bin/env python
# Interactive Python-GTK Console
# Copyright (C), 1998 James Henstridge <james@daa.com.au>
# Copyright (C),
# 1998 James Henstridge
# 2004 John Finlay
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -28,7 +30,9 @@
# have access to the list __history__, which is the command history.
import sys, string, traceback
import pango, gtk, gtk.keysyms
import pygtk
pygtk.require('2.0')
import gtk, pango
stdout = sys.stdout
......@@ -38,123 +42,149 @@ if not hasattr(sys, 'ps2'): sys.ps2 = '... '
# some functions to help recognise breaks between commands
def remQuotStr(s):
'''Returns s with any quoted strings removed (leaving quote marks)'''
ret = ''
in_quote = 0
quote = ''
prev = None
for ch in s:
if in_quote and (ch != quote or prev == '\\'):
if ch == '\\' and prev == '\\':
prev = None
else:
prev = ch
r = ''
inq = 0
qt = ''
prev = '_'
while len(s):
s0, s = s[0], s[1:]
if inq and (s0 != qt or prev == '\\'):
prev = s0
continue
prev = ch
if ch in '\'"':
if in_quote:
in_quote = 0
prev = s0
if s0 in '\'"':
if inq:
inq = 0
else:
in_quote = 1
quote = ch
ret = ret + ch
return ret
inq = 1
qt = s0
r = r + s0
return r
def bracketsBalanced(s):
'''Returns true iff the brackets in s are balanced'''
s = filter(lambda x: x in '()[]{}', s)
stack = []
brackets = {'(':')', '[':']', '{':'}'}
for ch in s:
if ch in '([{':
stack.append(ch)
elif ch in '}])':
if len(stack) != 0 and brackets[stack[-1]] == ch:
while len(s) != 0:
if s[0] in ")]}":
if len(stack) != 0 and brackets[stack[-1]] == s[0]:
del stack[-1]
else:
return 0
else:
stack.append(s[0])
s = s[1:]
return len(stack) == 0
class gtkoutfile:
'''A fake output file object. It sends output to a TK test widget,
'''A fake output file object. It sends output to a GTK TextView widget,
and if asked for a file number, returns one set on instance creation'''
def __init__(self, buffer, fileno, tag):
self.__fileno = fileno
self.__buffer = buffer
self.__tag = tag
def __init__(self, w, fn, font):
self.__fn = fn
self.__w = w
self.__b = w.get_buffer()
self.__ins = self.__b.get_mark('insert')
self.__font = font
def close(self): pass
flush = close
def fileno(self): return self.__fileno
def fileno(self): return self.__fn
def isatty(self): return 0
def read(self, a): return ''
def readline(self): return ''
def readlines(self): return []
def write(self, string):
def write(self, s):
#stdout.write(str(self.__w.get_point()) + '\n')
iter = self.__buffer.get_end_iter()
self.__buffer.insert_with_tags_by_name(iter, string, self.__tag)
def writelines(self, lines):
iter = self.__buffer.get_end_iter()
for line in lines:
self.__buffer.insert_with_tags_by_name(iter, lines, self.__tag)
iter = self.__b.get_iter_at_mark(self.__ins)
self.__b.insert_with_tags(iter, s, self.__font)
self.__w.scroll_to_mark(self.__ins, 0.0)
self.__w.queue_draw()
def writelines(self, l):
iter = self.__b.get_iter_at_mark(self.__ins)
for s in l:
self.__b.insert_with_tags(iter, s, self.__font)
self.__w.scroll_to_mark(self.__ins, 0.0)
self.__w.queue_draw()
def seek(self, a): raise IOError, (29, 'Illegal seek')
def tell(self): raise IOError, (29, 'Illegal seek')
truncate = tell
class Console(gtk.VBox):
def __init__(self, namespace={}, quit_cb=None):
def __init__(self, namespace={}, copyright='', greetings=(), quit_cb=None,
closer=True):
gtk.VBox.__init__(self, spacing=2)
self.set_border_width(2)
self.copyright = copyright
self.greetings = greetings
#self.set_size_request(475, 300)
self.quit_cb = quit_cb
swin = gtk.ScrolledWindow()
swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.pack_start(swin)
swin.show()
self.vadj = swin.get_vadjustment()
self.textview = gtk.TextView()
self.textview.set_editable(gtk.FALSE)
self.textview.set_cursor_visible(gtk.FALSE)
self.textview.set_wrap_mode(gtk.WRAP_WORD)
self.buffer = self.textview.get_buffer()
swin.add(self.textview)
self.textview.show()
# tags to use when inserting text ...
tag = self.buffer.create_tag('normal')
tag = self.buffer.create_tag('command')
tag.set_property('weight', pango.WEIGHT_BOLD)
tag = self.buffer.create_tag('error')
tag.set_property('style', pango.STYLE_ITALIC)
self.inp = gtk.HBox()
self.pack_start(self.inp)
self.inp.show()
self.scrolledwin = gtk.ScrolledWindow()
self.scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
self.scrolledwin.show()
self.inp.pack_start(self.scrolledwin, padding=2)
self.text = gtk.TextView()
self.text.set_editable(False)
self.text.set_wrap_mode(gtk.WRAP_WORD)
self.text.set_size_request(480, 400)
self.scrolledwin.add(self.text)
self.text.show()
self.buffer = self.text.get_buffer()
#create the tags we will use
self.normal = self.buffer.create_tag('Normal')
self.title = self.buffer.create_tag('Title',
weight=pango.WEIGHT_BOLD)
self.subtitle = self.buffer.create_tag('Subtitle',
scale=pango.SCALE_SMALL)
self.emphasis = self.buffer.create_tag('Emphasis',
style=pango.STYLE_OBLIQUE)
self.error = self.buffer.create_tag('Error',
foreground='red')
self.command = self.buffer.create_tag('Command')
self.inputbox = gtk.HBox(spacing=2)
self.pack_end(self.inputbox, expand=gtk.FALSE)
self.pack_end(self.inputbox, expand=False)
self.inputbox.show()
self.prompt = gtk.Label(sys.ps1)
self.prompt.set_padding(2, 0)
self.inputbox.pack_start(self.prompt, expand=gtk.FALSE)
self.prompt.set_padding(xpad=2, ypad=0)
self.prompt.set_size_request(30, -1)
self.inputbox.pack_start(self.prompt, fill=False, expand=False)
self.prompt.show()
self.closer = gtk.Button("Close")
self.closer.connect("clicked", self.quit)
self.inputbox.pack_end(self.closer, expand=gtk.FALSE)
self.closer.show()
if closer:
self.closer = gtk.Button("Close")
self.closer.connect("clicked", self.quit)
self.inputbox.pack_end(self.closer, fill=False, expand=False)
self.closer.show()
self.line = gtk.Entry()
self.line.set_size_request(400,-1)
self.line.connect("key_press_event", self.key_function)
self.inputbox.pack_start(self.line, padding=2)
self.line.show()
# now let the text box be resized
self.text.set_size_request(0, 0)
self.line.set_size_request(0, -1)
self.namespace = namespace
self.cmd = ''
self.cmd2 = ''
# set up hooks for standard output.
self.stdout = gtkoutfile(self.buffer, sys.stdout.fileno(), 'normal')
self.stderr = gtkoutfile(self.buffer, sys.stderr.fileno(), 'error')
self.stdout = gtkoutfile(self.text, sys.stdout.fileno(),
self.normal)
self.stderr = gtkoutfile(self.text, sys.stderr.fileno(),
self.error)
# set up command history
self.history = ['']
......@@ -162,9 +192,33 @@ class Console(gtk.VBox):
self.namespace['__history__'] = self.history
def init(self):
message = 'Python %s\n\n' % (sys.version,)
iter = self.buffer.get_end_iter()
self.buffer.insert_with_tags_by_name(iter, message, 'command')
self.text.realize()
self.insert = self.buffer.get_mark('insert')
iter = self.buffer.get_iter_at_mark(self.insert)
console_copyright = 'Interactive Python-GTK Console - \n' \
'Copyright (C)\n' \
'1998 James Henstridge\n' \
'2004 John Finlay'
greetings = (
('\n', self.subtitle),
('Python %s\n' % sys.version, self.title),
('\n%s\n\n' % sys.copyright, self.subtitle),
('%s\n\n' % console_copyright, self.subtitle),
('%s\n' % self.copyright, self.subtitle)
)
for greeting in greetings:
self.buffer.insert_with_tags(iter, *greeting)
self.greetings = (('Gimp-Python Console - ', 'Title'),
('Interactive Python Development\n', 'Emphasis'))
for greeting in self.greetings:
self.buffer.insert_with_tags_by_name(iter, *greeting)
self.text.scroll_to_mark(self.insert, 0.0)
self.line.grab_focus()
def quit(self, *args):
......@@ -174,35 +228,27 @@ class Console(gtk.VBox):
def key_function(self, entry, event):
if event.keyval == gtk.keysyms.Return:
self.line.emit_stop_by_name("key_press_event")
self.eval()
if event.keyval == gtk.keysyms.Tab:
self.line.emit_stop_by_name("key_press_event")
self.line.append_text('\t')
gtk.idle_add(self.focus_text)
return True
elif event.keyval == gtk.keysyms.Tab:
self.line.insert_text('\t', -1)
self.line.set_position(-1)
return True
elif event.keyval in (gtk.keysyms.KP_Up, gtk.keysyms.Up):
self.line.emit_stop_by_name("key_press_event")
self.historyUp()
gtk.idle_add(self.focus_text)
return True
elif event.keyval in (gtk.keysyms.KP_Down, gtk.keysyms.Down):
self.line.emit_stop_by_name("key_press_event")
self.historyDown()
gtk.idle_add(self.focus_text)
return True
elif event.keyval in (gtk.keysyms.D, gtk.keysyms.d) and \
event.state & gtk.gdk.CONTROL_MASK:
self.line.emit_stop_by_name("key_press_event")
event.state & gtk.gdk.CONTROL_MASK:
self.ctrld()
return True
def focus_text(self):
self.line.grab_focus()
return gtk.FALSE # don't requeue this handler
def scroll_bottom(self):
self.vadj.set_value(self.vadj.upper - self.vadj.page_size)
return gtk.FALSE
return False
def ctrld(self):
#self.quit()
pass
self.quit()
def historyUp(self):
if self.histpos > 0:
......@@ -212,6 +258,7 @@ class Console(gtk.VBox):
self.history[self.histpos] = l
self.histpos = self.histpos - 1
self.line.set_text(self.history[self.histpos])
self.line.set_position(-1)
def historyDown(self):
if self.histpos < len(self.history) - 1:
......@@ -232,9 +279,9 @@ class Console(gtk.VBox):
else:
self.history[self.histpos] = l
self.line.set_text('')
iter = self.buffer.get_end_iter()
self.buffer.insert_with_tags_by_name(iter, self.prompt.get_text() + l,
'command')
iter = self.buffer.get_iter_at_mark(self.insert)
self.buffer.insert_with_tags(iter, self.prompt.get() + l, self.command)
self.text.scroll_to_mark(self.insert, 0.0)
if l == '\n':
self.run(self.cmd)
self.cmd = ''
......@@ -246,7 +293,7 @@ class Console(gtk.VBox):
self.cmd2 = self.cmd2 + remQuotStr(l)
l = string.rstrip(l)
if not bracketsBalanced(self.cmd2) or l[-1] == ':' or \
l[-1] == '\\' or l[0] in ' \11':
l[-1] == '\\' or l[0] in ' \11':
self.prompt.set_text(sys.ps2)
self.prompt.queue_draw()
return
......@@ -260,41 +307,42 @@ class Console(gtk.VBox):
try:
try:
r = eval(cmd, self.namespace, self.namespace)
self.namespace['_'] = r
if r is not None:
print `r`
except SyntaxError:
exec cmd in self.namespace
except:
if hasattr(sys, 'last_type') and \
sys.last_type == SystemExit:
sys.last_type == SystemExit:
self.quit()
else:
traceback.print_exc()
self.prompt.set_text(sys.ps1)
self.prompt.queue_draw()
gtk.idle_add(self.scroll_bottom)
sys.stdout, self.stdout = self.stdout, sys.stdout
sys.stderr, self.stderr = self.stderr, sys.stderr
def gtk_console(ns, title='Python', menu=None):
def gtk_console(ns, title='Python', copyright='', menu=None):
win = gtk.Window()
win.set_default_size(475, 300)
win.connect("destroy", gtk.mainquit)
win.connect("delete_event", gtk.mainquit)
win.set_size_request(475, 500)
win.connect("destroy", lambda w: gtk.main_quit())
win.connect("delete_event", lambda w,e: gtk.main_quit())
win.set_title(title)
cons = Console(namespace=ns, quit_cb=gtk.mainquit)
cons = Console(namespace=ns, copyright=copyright,
quit_cb=lambda: gtk.main_quit())
if menu:
box = gtk.VBox()
win.add(box)
box.show()
box.pack_start(menu, expand=gtk.FALSE)
box.pack_start(menu, expand=False)
menu.show()
box.pack_start(cons)
else:
win.add(cons)
cons.show()
win.show()
cons.init()
gtk.main()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment