GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Aug 23, 2014 7:49 pm

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: signals of destroying window
PostPosted: Thu Dec 22, 2011 5:46 pm 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
Hi!

I've a window which I close by pressing X, and I want to know when it will disappear from screen. Signals 'destroy' and 'delete-event' don't work that way.

Thanks.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Fri Dec 23, 2011 5:38 am 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
please provide more information to your problem!!!


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Fri Dec 23, 2011 12:43 pm 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
Code:
#!/usr/bin/env python

from gi.repository import Gtk
import time

def callback(signal, close=False):
    print 'got signal: "%s", window is still present on the screen' % (signal,)
    time.sleep(5)
    if close: Gtk.main_quit()

w = Gtk.Window()
w.set_default_size(400, 300)
w.set_position(Gtk.WindowPosition.CENTER)

w.connect('delete-event', lambda w,e: callback('delete-event'))
w.connect('destroy', lambda w: callback('destroy', True))

w.show_all()
Gtk.main()


I want to execute code when the window is NOT present on screen.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sat Dec 24, 2011 3:32 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Maybe "unrealize" signal will help.

_________________
* PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
* Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sat Dec 24, 2011 8:47 pm 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
Unfortunately not.

connect_after is not helping either.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sun Dec 25, 2011 8:50 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hi,
Screen redraws are performed during idle time in the main loop. So if you want to wait for the screen to be redraw with out the window and then execute some code you will need to add an idle call back, but at a lower priority. What will happen is the normal signals will occur when we ask for the window to be destroyed, on the "destroy" signal we add an idle call back. We then let control return back to the main loop. All other idle call backs happen that deal with redraws, then our idle call back gets run after all the others as it has a lower priority.

Below is you code rewritten to demonstrate this.
Code:
#!/usr/bin/env python

from gi.repository import Gtk
import glib
import time

def callback(signal, close=False):
    print 'got signal: "%s", window is still present on the screen' % (signal,)
    time.sleep(1)
    if close: glib.idle_add(idle_callback)
    return False

def idle_callback():
    print 'got idle signal'
    time.sleep(5)
    Gtk.main_quit()
    return False

w = Gtk.Window()
w.set_default_size(400, 300)
w.set_position(Gtk.WindowPosition.CENTER)

w.connect('delete-event', lambda w,e: callback('delete-event'))
w.connect('destroy', lambda w: callback('destroy', True))

w.show_all()
Gtk.main()

_________________
E.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sun Dec 25, 2011 11:00 am 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
It works :) Thank You.

PS.
Code:
#import glib
from gi.repository import GLib


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sun Dec 25, 2011 11:11 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
I don't normally venture into Python, I normally stick with C and C++. But the principals of GTK+ is the same.

_________________
E.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Tue Dec 27, 2011 12:02 pm 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
Something strange is happening.
Window is gone from screen, but it will be present on screenshot.

Code:
#!/usr/bin/env python

from gi.repository import Gtk, GLib, Gdk
import time

def destroy_callback(widget):
    print 'got destroy signal'
    time.sleep(3)
    GLib.idle_add(idle_callback)

def idle_callback():
    print 'got idle signal'

    rw = Gdk.get_default_root_window()
    rw_w = rw.get_width()
    rw_h = rw.get_height()
    p = Gdk.pixbuf_get_from_window(rw, 0, 0, rw_w, rw_h)
    i = Gtk.Image.new_from_pixbuf(p)
    w = Gtk.Window()
    w.add(i)
    w.show_all()
    w.connect('destroy', lambda w: Gtk.main_quit())
    return False

w = Gtk.Window()
w.set_default_size(400, 300)
w.set_position(Gtk.WindowPosition.CENTER)

w.connect('destroy', destroy_callback)

w.show_all()
Gtk.main()


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sat Jan 07, 2012 11:55 pm 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
Could anyone help me? Why is this happening? Window is still present on screenshot, what I'm doing wrong?

Nothing is working
Code:
while (Gtk.events_pending()) {
            Gtk.main_iteration();
}
Gdk.Display.get_default().flush();
Gdk.get_default_root_window().process_updates(true);
Gdk.Window.process_all_updates();


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sun Jan 08, 2012 9:45 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
I am really not that good at Python, but give this a go. What I think is happening is although our redraws were done when we did the screen shot and had nothing else to do, other applications were still doing their redraws and they still had the remains of our windows image. To get around this I have used a time-out of 1 second instead of waiting for our work to be done. Hopefully in this time we would have done our work to destroy the window and the other applications would have done their redraws.

I am not sure their is a way to wait for all other applications to do their redraws. If there is then I am sure it would be pretty low level possibly at the X11 level.

Code:
#!/usr/bin/env python

from gi.repository import Gtk, GLib, Gdk
import time

def destroy_callback(widget):
    print 'got destroy signal'
    GLib.timeout_add(1000, timeout_callback)

def timeout_callback():
    print 'got timout signal'

    rw = Gdk.get_default_root_window()
    rw_w = rw.get_width()
    rw_h = rw.get_height()
    p = Gdk.pixbuf_get_from_window(rw, 0, 0, rw_w, rw_h)
    image = p.scale_simple(rw_w / 2, rw_h / 2, 2)
    i = Gtk.Image.new_from_pixbuf(image)
    w = Gtk.Window()
    w.add(i)
    w.show_all()
    w.connect('destroy', lambda w: Gtk.main_quit())
    return False

w = Gtk.Window()
w.set_default_size(400, 300)
w.set_position(Gtk.WindowPosition.CENTER)

w.connect('destroy', destroy_callback)

w.show_all()
Gtk.main()

_________________
E.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Sun Jan 08, 2012 10:17 am 
Offline
Familiar Face

Joined: Thu Dec 22, 2011 5:38 pm
Posts: 7
Yes, it is working, but in my application time is very important and I can't wait 1sec. Unfortunatelly I know X11 programming less than GTK.


Top
 Profile  
 
 Post subject: Re: signals of destroying window
PostPosted: Mon Jan 09, 2012 7:26 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
I have done some experiments, would 60 ms (0.06 second) be fine for you as this works for me on my fairly old machine.

You just need to change the line
Code:
    GLib.timeout_add(1000, timeout_callback)
to
Code:
    GLib.timeout_add(60, timeout_callback)

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group