On Mon, Jul 22, 2002 at 03:04:54PM +0200, Pier Carteri wrote:
> I'm writing a simple editor, and I want that when a user press a
> tab-key this char is automatically replaced with some spaces. I've
> tried to added a callback to the "insert-text" signal but using this
> event I obtain a buffer with some spaces after a tab (instead of one
> with only spaces). Which signal should i use?
Pier, I agree to assist you only because you are actively helping the
global movement to stop tabs entering code: DON'T USE TABS (tm). Those
pesky characters show up everywhere, and completely ruin things for
projects with many users.
I've added faq question 14.5 which exemplifies what you need to do. It
was tested on a GtkEntry(), but should work for any editable. Write back
if it doesn't. I'm pasting the text here:
--- xxx ---
14.5. I want to change the text being typed into an entry or textbox,
but the original text is also inserted! **
A common need is to handle text being inserted into an entry or textbox,
but change that text on-the-fly. As an example, Pier Carteri wrote the
mailing list asking how he could expand tabs typed into spaces; however,
the original tab was still being inserted.
The general solution to this is to use two functions, one as a "proxy"
callback that calls emit_stop_by_name() and idle_add()s the real
function. It could work something like this:
def insert_cb(widget, text, length, *args):
# if you don't do this, garbage comes in with text
text = text[:length]
pos = widget.get_position()
# stop default emission
widget.emit_stop_by_name("insert_text")
gtk.idle_add(insert, widget, text, pos)
def insert(widget, text, pos):
# the next three lines set up the text. this is done because we
# can't use insert_text(): it always inserts at position zero.
orig_text = widget.get_text()
text = string.replace(text, " ", "<SPACE>")
new_text = orig_text[:pos] + text + orig_text[pos:]
# avoid recursive calls triggered by set_text
widget.signal_handler_block(insert_sig)
# replace the text with some new text
widget.set_text(new_text)
widget.signal_handler_unblock(insert_sig)
# set the correct position in the widget
widget.set_position(pos + len(text))
entry = gtk.GtkEntry()
signal = entry.connect("insert_text", insert_cb)
Some notes about this approach:
* We need to do some funky stuff when using set_text() in insert()
regarding the position of the text being inserted. This is because
insert_text() doesn't provide a position argument in pygtk-0.x
* Signals need to be blocked inside insert() or we end up with a
recursive call, with text being inserted triggering the callback.
* There could be nicer solutions, but this one works.
* This originally comes from Ricardo Lenzi's code that was contributed
to Kiwi, and is used in the Kiwi Entry wrapper.
--- xxx ---
Take care,
--
Christian Reis, Senior Engineer, Async Open Source, Brazil.
http://async.com.br/~kiko/ | [+55 16] 261 2331 | NMFL
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/