[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby-gnome2 : problem with signal "insert-text"

oxman

1/13/2005 9:57:00 PM

Hello,

i have a problem with the signal "insert-text", the signal was
call before the text is insert, so i can't color it, how i can
have a signal just after the text have been insert ?

thanks
oxman.
21 Answers

Michael C. Libby

1/14/2005 12:47:00 PM

0

On Fri, Jan 14, 2005 at 07:01:14AM +0900, oxman wrote:
> Hello,
>
> i have a problem with the signal "insert-text", the signal was
> call before the text is insert, so i can't color it, how i can
> have a signal just after the text have been insert ?

Are you sure? insert-text should be getting called for every text
insertion--and it can't really be called until an insertion occurs.
If you are typing, every letter typed would cause this signal to emit.

Perhaps a code sample would help illustrate the issue.

You might try connecting to "changed" signal and then test the
buffer for words you want to color and then apply the coloring
at that time.

You could do the same with "insert-text" signal, and keep track
of letters typed that *could* be a word you'd want to color, and
then when the word is complete go back and color the whole word.

>
> thanks
> oxman.
>

- Michael


Peter Stuifzand

1/14/2005 1:24:00 PM

0

On Fri, 14 Jan 2005 07:01:14 +0900, oxman <no@in-your-dream.net> wrote:
> Hello,
>
> i have a problem with the signal "insert-text", the signal was
> call before the text is insert, so i can't color it, how i can
> have a signal just after the text have been insert ?
>
> thanks
> oxman.
>
>

Hello oxman,

You should use:

widget.signal_connect_after("nsert-text") do
# code ....
end

--
Peter


oxman

1/14/2005 2:38:00 PM

0

Thanks.
But the time-out to call 'insert-text' is "very long".

buffer.signal_connect_after("insert-text") do |a,first_char,text,length|
if (text == "#")
first_char.offset = first_char.offset - 1
last_char = buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end

buffer.apply_tag(buffer.tag_table.lookup("blue"),first_char, last_char)
end
end

The text is colored one or two seconds after i write the '#' character.

How i can have a signal immediately called after "insert-text" ?


Peter Stuifzand wrote:
> On Fri, 14 Jan 2005 07:01:14 +0900, oxman <no@in-your-dream.net> wrote:
>
>>Hello,
>>
>>i have a problem with the signal "insert-text", the signal was
>>call before the text is insert, so i can't color it, how i can
>>have a signal just after the text have been insert ?
>>
>>thanks
>>oxman.
>>
>>
>
>
> Hello oxman,
>
> You should use:
>
> widget.signal_connect_after("nsert-text") do
> # code ....
> end
>

oxman

1/15/2005 9:35:00 AM

0

Humm, after many test, I see my code is executed immediately.
But the effect of my code isn't immediat.
I think 'buffer' or 'textview' have a refresh period.
How i can force the refresh for 'buffer' or 'textview' ?


oxman wrote:
> Thanks.
> But the time-out to call 'insert-text' is "very long".
>
> buffer.signal_connect_after("insert-text") do |a,first_char,text,length|
> if (text == "#")
> first_char.offset = first_char.offset - 1
> last_char = buffer.get_iter_at_offset(first_char.offset)
> last_char.forward_to_line_end
>
> buffer.apply_tag(buffer.tag_table.lookup("blue"),first_char, last_char)
> end
> end
>
> The text is colored one or two seconds after i write the '#' character.
>
> How i can have a signal immediately called after "insert-text" ?
>
>
> Peter Stuifzand wrote:
>
>> On Fri, 14 Jan 2005 07:01:14 +0900, oxman <no@in-your-dream.net> wrote:
>>
>>> Hello,
>>>
>>> i have a problem with the signal "insert-text", the signal was
>>> call before the text is insert, so i can't color it, how i can
>>> have a signal just after the text have been insert ?
>>>
>>> thanks
>>> oxman.
>>>
>>>
>>
>>
>> Hello oxman,
>>
>> You should use:
>>
>> widget.signal_connect_after("nsert-text") do
>> # code .... end
>>

Michael C. Libby

1/15/2005 11:57:00 AM

0

On Sat, Jan 15, 2005 at 06:36:17PM +0900, oxman wrote:
> Humm, after many test, I see my code is executed immediately.
> But the effect of my code isn't immediat.
> I think 'buffer' or 'textview' have a refresh period.
> How i can force the refresh for 'buffer' or 'textview' ?

I've never noticed any sort refresh issues with the text
widgets, but...

You can always make your application do all pending Gtk events
with a function like:

def update_window
while Gtk.event_pending? do
Gtk.main_iteration
end
end

I should note that while this works well for me on Linux,
it does cause the program to hang on Windows.

- Michael

oxman

1/15/2005 1:20:00 PM

0

It don't work.
The text is colored after one or two second :(

Michael C. Libby wrote:
> On Sat, Jan 15, 2005 at 06:36:17PM +0900, oxman wrote:
>
>>Humm, after many test, I see my code is executed immediately.
>>But the effect of my code isn't immediat.
>>I think 'buffer' or 'textview' have a refresh period.
>>How i can force the refresh for 'buffer' or 'textview' ?
>
>
> I've never noticed any sort refresh issues with the text
> widgets, but...
>
> You can always make your application do all pending Gtk events
> with a function like:
>
> def update_window
> while Gtk.event_pending? do
> Gtk.main_iteration
> end
> end
>
> I should note that while this works well for me on Linux,
> it does cause the program to hang on Windows.
>
> - Michael

Michael C. Libby

1/15/2005 1:58:00 PM

0

On Sat, Jan 15, 2005 at 10:21:10PM +0900, oxman wrote:
> It don't work.
> The text is colored after one or two second :(

Do you have a short working example that demonstrates
the issue? Hard to debug without code. :)

-Michael

oxman

1/15/2005 2:15:00 PM

0

The code :

#!/usr/bin/ruby1.8
require 'gtk2'
Gtk.init
window = Gtk::Window.new
window.signal_connect("destroy") {
puts "destroy"
Gtk.main_quit
}
textviewer = Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer = textviewer.buffer
buffer.create_tag("blue", {"foreground" => "blue"})
buffer.signal_connect_after('insert-text') do |a,first_char,text,length|
if (text == '#')
first_char.offset -= 1
last_char = buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end
buffer.apply_tag(buffer.tag_table.lookup("blue"),
first_char, last_char)
update_window
end
end

def update_window
while Gtk.events_pending? do
Gtk.main_iteration
end
end
Gtk.main


Michael C. Libby wrote:
> On Sat, Jan 15, 2005 at 10:21:10PM +0900, oxman wrote:
>
>>It don't work.
>>The text is colored after one or two second :(
>
>
> Do you have a short working example that demonstrates
> the issue? Hard to debug without code. :)
>
> -Michael

Michael C. Libby

1/15/2005 4:29:00 PM

0

On Sat, Jan 15, 2005 at 11:21:13PM +0900, oxman wrote:
> The code :
>
> #!/usr/bin/ruby1.8
> require 'gtk2'
> Gtk.init
> window = Gtk::Window.new
> window.signal_connect("destroy") {
> puts "destroy"
> Gtk.main_quit
> }
> textviewer = Gtk::TextView.new
> window.add(textviewer)
> window.set_default_size(600,400)
> window.show_all
> buffer = textviewer.buffer
> buffer.create_tag("blue", {"foreground" => "blue"})
> buffer.signal_connect_after('insert-text') do |a,first_char,text,length|
> if (text == '#')
> first_char.offset -= 1
> last_char = buffer.get_iter_at_offset(first_char.offset)
> last_char.forward_to_line_end
> buffer.apply_tag(buffer.tag_table.lookup("blue"),
> first_char, last_char)
> update_window
> end
> end
>
> def update_window
> while Gtk.events_pending? do
> Gtk.main_iteration
> end
> end
> Gtk.main

I guess in this case update_window isn't going to have an effect because
no Gtk events are actually pending.

I can only see this problem when I type # and then wait. If I continue to
type or quickly switch lines using the arrow keys then it changes the color
right away. So the problem is that it's not detecting the need for an
update very quickly. I think this has to do with the priority of redraws.

I did find information about redraw priority here:
http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ATextView#PRIORIT...

I have not found any way to adjust the priority of redraws in the Ruby-
GNOME2 docs or in the regular GNOME API docs. But I thought I'd mention it
in case it help you find more info.

You might look at GtkSourceView as well... if not for using it, to see
how it handles this problem.

-Michael

Joao Pedrosa

1/15/2005 5:06:00 PM

0

Hi,

On Sat, 15 Jan 2005 23:21:13 +0900, oxman <no@in-your-dream.net> wrote:
> The code :

Do you get any improvement or idea with this code:

require 'gtk2'
Gtk.init
window = Gtk::Window.new
window.signal_connect("destroy") { Gtk.main_quit }
textviewer = Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer = textviewer.buffer
buffer.create_tag("blue", {"foreground" => "blue"})
buffer.signal_connect('insert-text') do |a,iter,text,length|
Gtk.timeout_add(10){
iter = buffer.start_iter
while a = iter.forward_search('#', Gtk::TextIter::SEARCH_TEXT_ONLY) do
iter, = a
iter2 = buffer.get_iter_at_offset(iter.offset)
iter2.forward_to_line_end
buffer.apply_tag(buffer.tag_table.lookup("blue"), iter, iter2)
iter.forward_line
p a
end
false
}
end
Gtk.main

It seems that after the first # is colored I get some improvement, but
I haven't come up with a best approach yet, though I'm interested in
this as well. :-)

Cheers,
Joao