]> git.tdb.fi Git - libs/gui.git/commitdiff
React to resize and close events
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Aug 2007 08:32:07 +0000 (08:32 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 26 Aug 2007 08:32:07 +0000 (08:32 +0000)
source/glwindow.cpp
source/glwindow.h
source/window.cpp
source/window.h

index 530d33719f94567111fba56969fcfc3d3134d88e..3ae559050a406dcdfe6aeddb43925d9fa612a050 100644 (file)
@@ -11,8 +11,6 @@ Distributed under the LGPL
 
 using namespace std;
 
-#include <iostream>
-
 namespace Msp {
 
 GLDisplayOptions::GLDisplayOptions():
@@ -38,7 +36,7 @@ GLWindow::GLWindow(const DisplayOptions &dopt, const GLDisplayOptions &gl_dopt)
 
 GLWindow::~GLWindow()
 {
-       glXMakeCurrent(display, window, 0);
+       glXMakeCurrent(display, 0, 0);
        glXDestroyContext(display, context);
 }
 
@@ -77,13 +75,14 @@ void GLWindow::init()
        if(!context)
                throw Exception("Couldn't create a GLX context");
 
-       window=XCreateWindow(display, DefaultRootWindow(display), 0, 0, options.width, options.height, 0, CopyFromParent, InputOutput, CopyFromParent, 0, 0);
-       if(!window)
-               throw Exception("Couldn't create a window");
-
-       XSelectInput(display, window, ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask);
+       create();
 
        glXMakeCurrent(display, window, context);
 }
 
+void GLWindow::on_resize()
+{
+       glViewport(0, 0, options.width, options.height);
+}
+
 } // namespace Msp
index f79c673f7dbdd8970dfe42fd0f74dd1c7b1c0d68..35254391de81390a0a0db5090175aaf5726a0885 100644 (file)
@@ -38,6 +38,7 @@ public:
        void swap_buffers();
 protected:
        void init();
+       virtual void on_resize();
 };
 
 } // namespace Msp
index 75a88dce6f3331e82917ea161de7bc604a67b5e7..b977f6bc97ac89cbd7af1e81d02f125593f3de02 100644 (file)
@@ -6,14 +6,12 @@ Distributed under the LGPL
 */
 
 #include <vector>
-#include <GL/glx.h>
+#include <X11/Xutil.h>
 #include <msp/core/error.h>
 #include "window.h"
 
 using namespace std;
 
-#include <msp/strings/lexicalcast.h>
-
 namespace Msp {
 
 DisplayOptions::DisplayOptions():
@@ -90,18 +88,26 @@ void Window::prepare()
        if(!display)
                throw Exception("Couldn't open X display");
 
+       wm_delete_window=XInternAtom(display, "WM_DELETE_WINDOW", true);
+
        //XSetErrorHandler(x_error_handler);
 }
 
-void Window::init()
+void Window::create()
 {
-       prepare();
-
        window=XCreateWindow(display, DefaultRootWindow(display), 0, 0, options.width, options.height, 0, CopyFromParent, InputOutput, CopyFromParent, 0, 0);
        if(!window)
                throw Exception("Couldn't create a window");
 
        XSelectInput(display, window, ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask);
+
+       XSetWMProtocols(display, window, &wm_delete_window, 1);
+}
+
+void Window::init()
+{
+       prepare();
+       create();
 }
 
 void Window::process_event(const XEvent &event)
@@ -131,6 +137,12 @@ void Window::process_event(const XEvent &event)
        case ConfigureNotify:
                options.width=event.xconfigure.width;
                options.height=event.xconfigure.height;
+               on_resize();
+               signal_resize.emit(options.width, options.height);
+               break;
+       case ClientMessage:
+               if(event.xclient.data.l[0]==static_cast<long>(wm_delete_window))
+                       signal_close.emit();
                break;
        default:;
        }
index 6f1f0d1e26bf5cd4db7d8c32cefe86b9bb26acc1..34efb7ce888a04d399e918bbab778a8dbdc25663 100644 (file)
@@ -32,6 +32,8 @@ public:
        sigc::signal<void, int, int> signal_pointer_motion;
        sigc::signal<void, unsigned, unsigned, unsigned> signal_key_press;
        sigc::signal<void, unsigned, unsigned> signal_key_release;
+       sigc::signal<void, unsigned, unsigned> signal_resize;
+       sigc::signal<void> signal_close;
 
 protected:
        typedef ::Window Handle;
@@ -39,6 +41,7 @@ protected:
        Display *display;
        DisplayOptions options;
        Handle  window;
+       Atom    wm_delete_window;
 
        Window();
 public:
@@ -53,8 +56,10 @@ public:
        void tick();
 protected:
        void prepare();
+       void create();
        void init();
        void process_event(const XEvent &);
+       virtual void on_resize() { }
 
        static int x_error_handler(Display *, XErrorEvent *);
 };