Re: Existence Announcement




On Apr 5, 2005, at 9:52 PM, ADIS - C.P.D. wrote:

How to identify a window to known whether it already exists?
I want to prevent multiple instances of the same window.

is that multiple instances of the same window within the same process, or you want to prevent multiple processes?

within the same process is easy -- use Gtk2::Window::list_toplevels() to get a list of all the toplevel windows in your app, and search through that for a given window.


multiple processes will involve some form of IPC. i've never done this with X, but for some reason i can never resist a challenge, so i poked around a bit in the mozilla sources to see how mozilla's remote commands work... there's a remote-helper, a tiny program that links to Xlib. this program uses XQueryTree() to fetch a list of all windows, then iterates through those windows, using XGetWindowProperty() to look for the first one that has certain Atoms set. when it finds this window (which belongs to a running mozilla instance), it uses XChangeProperty() to change the value of the Atom on which mozilla will be listening for commands. the X server sends an event to the owner of the window, which happens to be mozilla, who responds by parsing and executing the command. at this point, the remote-helper is usually already gone.

gdk_property_get() and gdk_property_change(), the wrappers for XGetWindowProperty() and XChangeProperty(), are bound in gtk2-perl as Gtk2::Gdk::Window::property_get() and Gtk2::Gdk::Window::property_change(), respectively.

but gdk does not expose a direct wrapper for XQueryTree(), so there is no way to search all of the windows on the display for one with the desired property. the X11::Protocol module on CPAN provides this functionality, or, if you already have some xs in your app, you could very easily create your own function to do this with Xlib.


on win32, WinMain gets the instance handle and previous instance handle, so you can decide whether to create a new window or message the old one. i haven't looked into it, but i suspect the win32 port of gtk+ doesn't expose this either, and you'd have to use the Win32 module.


i'm sure there are other ways, but i don't know off the top of my head.


--
I believe that if music companies are going to set examples they need to do it to appropriate people and not dead people.
  --  Robin Chianumba




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]