From 935c50bdcc95fe95d39931182461a2b25ac30b9d Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 Aug 2007 08:32:07 +0000 Subject: [PATCH] React to resize and close events --- source/glwindow.cpp | 15 +++++++-------- source/glwindow.h | 1 + source/window.cpp | 24 ++++++++++++++++++------ source/window.h | 5 +++++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/source/glwindow.cpp b/source/glwindow.cpp index 530d337..3ae5590 100644 --- a/source/glwindow.cpp +++ b/source/glwindow.cpp @@ -11,8 +11,6 @@ Distributed under the LGPL using namespace std; -#include - 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 diff --git a/source/glwindow.h b/source/glwindow.h index f79c673..3525439 100644 --- a/source/glwindow.h +++ b/source/glwindow.h @@ -38,6 +38,7 @@ public: void swap_buffers(); protected: void init(); + virtual void on_resize(); }; } // namespace Msp diff --git a/source/window.cpp b/source/window.cpp index 75a88dc..b977f6b 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -6,14 +6,12 @@ Distributed under the LGPL */ #include -#include +#include #include #include "window.h" using namespace std; -#include - 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(wm_delete_window)) + signal_close.emit(); break; default:; } diff --git a/source/window.h b/source/window.h index 6f1f0d1..34efb7c 100644 --- a/source/window.h +++ b/source/window.h @@ -32,6 +32,8 @@ public: sigc::signal signal_pointer_motion; sigc::signal signal_key_press; sigc::signal signal_key_release; + sigc::signal signal_resize; + sigc::signal 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 *); }; -- 2.43.0