LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Prevent XLIB application from closing when close button pressed (https://www.linuxquestions.org/questions/programming-9/prevent-xlib-application-from-closing-when-close-button-pressed-598493/)

fcdev 11-09-2007 09:55 PM

Prevent XLIB application from closing when close button pressed
 
Anybody know how I can prevent the close button (the top right X in the title bar) from closing my application. I want to ask the user if they wish to save their changes before closing (and perhaps even to cancel the close operation).

If it makes a difference in the answer ... I want the close button to change a variable from 0 to 1, call a particular function, and then for the application to return to normal operation as though nothing had ever happened. The application would perform an elegant shutdown from there if it is still needed.

My application is running in a plain Xlib window with an OpenGL render (no GTK, or other libraries are linked in).

Thanks for any help you can provide

paulsm4 11-11-2007 01:47 PM

Hi -

Here are a couple of links from people who've asked similar (the same?) questions:

http://www.linuxquestions.org/questi...43#post2747343

http://www.linuxquestions.org/questi...95#post2188995

See also comp.windows.x FAQ #188:
http://www.faqs.org/faqs/x-faq/part7/section-40.html
<= Good explanation of what's going on...

The basic issue is that the little "X" (close window) icon isn't really an Xlib thing; it's actually more of a Windows manager thing.

Here is one possible solution:
http://groups.google.com/group/comp....44530709ea65aa

Code:

...
  Atom              wm_protocols;
  Atom              wm_delete_window;
  ...
  wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
  wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  XSetWMProtocols(dpy, win, &wm_delete_window, 1);
  ...
  for (;;) {
    XEvent event;

    XNextEvent(dpy, &event);
    switch (event.type) {
      case ClientMessage:
        if (event.xclient.message_type == wm_protocols &&
            event.xclient.data.l[0] == wm_delete_window)  {
          XDestroyWindow(dpy, win);
          XCloseDisplay(dpy);
          exit(0);
        }
        break;
      ...
      default:
        break;
    }
  }

'Hope that helps .. PSM


All times are GMT -5. The time now is 07:14 AM.