summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2016-10-28 17:55:06 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2016-10-28 17:55:06 +0200
commit05b3e8cdc7f6cf7056c96d9cd150a0e2a8fb85a4 (patch)
tree19c2723984cef60a8000812dc4fb5176dcba42d0
parente19d48332a7bc8963bc0a58d746a75dc15eeb58c (diff)
Rewrite event handler to use shared_ptr Events instead of raw pointers.
-rw-r--r--plugingui/eventhandler.cc27
-rw-r--r--plugingui/eventhandler.h5
-rw-r--r--plugingui/nativewindow.h5
-rw-r--r--plugingui/nativewindow_win32.cc22
-rw-r--r--plugingui/nativewindow_win32.h6
-rw-r--r--plugingui/nativewindow_x11.cc23
-rw-r--r--plugingui/nativewindow_x11.h6
7 files changed, 43 insertions, 51 deletions
diff --git a/plugingui/eventhandler.cc b/plugingui/eventhandler.cc
index 62ffb2b..083efae 100644
--- a/plugingui/eventhandler.cc
+++ b/plugingui/eventhandler.cc
@@ -43,12 +43,12 @@ bool EventHandler::hasEvent()
return nativeWindow.hasEvent();
}
-Event *EventHandler::getNextEvent()
+std::shared_ptr<Event> EventHandler::getNextEvent()
{
return nativeWindow.getNextEvent();
}
-Event *EventHandler::peekNextEvent()
+std::shared_ptr<Event> EventHandler::peekNextEvent()
{
return nativeWindow.peekNextEvent();
}
@@ -87,16 +87,13 @@ void EventHandler::processEvents()
}
if(peekEvent->type() != EventType::resize)
{
- //delete peekEvent;
break;
}
- //delete peekEvent;
- //delete event;
event = getNextEvent();
}
- auto resizeEvent = static_cast<ResizeEvent*>(event);
+ auto resizeEvent = static_cast<ResizeEvent*>(event.get());
if((resizeEvent->width != window.width()) ||
(resizeEvent->height != window.height()))
{
@@ -121,16 +118,13 @@ void EventHandler::processEvents()
}
if(peekEvent->type() != EventType::mouseMove)
{
- //delete peekEvent;
break;
}
- //delete peekEvent;
- //delete event;
event = getNextEvent();
}
- auto moveEvent = static_cast<MouseMoveEvent*>(event);
+ auto moveEvent = static_cast<MouseMoveEvent*>(event.get());
auto widget = window.find(moveEvent->x, moveEvent->y);
auto oldwidget = window.mouseFocus();
@@ -178,7 +172,7 @@ void EventHandler::processEvents()
continue;
}
- auto buttonEvent = static_cast<ButtonEvent*>(event);
+ auto buttonEvent = static_cast<ButtonEvent*>(event.get());
lastWasDoubleClick = buttonEvent->doubleClick;
@@ -236,19 +230,16 @@ void EventHandler::processEvents()
}
if(peekEvent->type() != EventType::scroll)
{
- //delete peekEvent;
break;
}
- //delete peekEvent;
- auto scrollEvent = static_cast<ScrollEvent*>(event);
+ auto scrollEvent = static_cast<ScrollEvent*>(event.get());
delta += scrollEvent->delta;
- //delete event;
event = getNextEvent();
}
- auto scrollEvent = static_cast<ScrollEvent*>(event);
+ auto scrollEvent = static_cast<ScrollEvent*>(event.get());
scrollEvent->delta += delta;
auto widget = window.find(scrollEvent->x, scrollEvent->y);
@@ -267,7 +258,7 @@ void EventHandler::processEvents()
// TODO: Filter out multiple arrow events.
- auto keyEvent = static_cast<KeyEvent*>(event);
+ auto keyEvent = static_cast<KeyEvent*>(event.get());
if(window.keyboardFocus())
{
window.keyboardFocus()->keyEvent(keyEvent);
@@ -279,8 +270,6 @@ void EventHandler::processEvents()
closeNotifier();
break;
}
-
- delete event;
}
}
diff --git a/plugingui/eventhandler.h b/plugingui/eventhandler.h
index 490c515..6269baf 100644
--- a/plugingui/eventhandler.h
+++ b/plugingui/eventhandler.h
@@ -27,6 +27,7 @@
#pragma once
#include <notifier.h>
+#include <memory>
#include "guievent.h"
#include "nativewindow.h"
@@ -47,11 +48,11 @@ public:
//! \brief Get a single event from the event queue.
//! \return A pointer to the event or nullptr if there are none.
- Event *getNextEvent();
+ std::shared_ptr<Event> getNextEvent();
//! \brief Get a single event from the event queue without popping it.
//! \return A pointer to the event or nullptr if there are none.
- Event *peekNextEvent();
+ std::shared_ptr<Event> peekNextEvent();
Notifier<> closeNotifier;
diff --git a/plugingui/nativewindow.h b/plugingui/nativewindow.h
index a7d101e..ff24fed 100644
--- a/plugingui/nativewindow.h
+++ b/plugingui/nativewindow.h
@@ -27,6 +27,7 @@
#pragma once
#include <string>
+#include <memory>
#include "guievent.h"
@@ -74,11 +75,11 @@ public:
//! \brief Read a single event from the event queue.
//! \return A pointer to the event or nullptr is none exists.
- virtual Event *getNextEvent() = 0;
+ virtual std::shared_ptr<Event> getNextEvent() = 0;
//! \brief Read next event without popping it from the event queue.
//! \return A pointer to the event or nullptr is none exists.
- virtual Event* peekNextEvent() = 0;
+ virtual std::shared_ptr<Event> peekNextEvent() = 0;
};
} // GUI::
diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc
index f49f3c6..b318f59 100644
--- a/plugingui/nativewindow_win32.cc
+++ b/plugingui/nativewindow_win32.cc
@@ -52,7 +52,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
static bool first = true;
if(!first)
{
- ResizeEvent* resizeEvent = new ResizeEvent();
+ auto resizeEvent = std::make_shared<ResizeEvent>();
resizeEvent->width = LOWORD(lp);
resizeEvent->height = HIWORD(lp);
native->event_queue.push(resizeEvent);
@@ -63,7 +63,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
case WM_MOVE:
{
-// MoveEvent* moveEvent = new MoveEvent();
+// auto moveEvent = std::make_shared<MoveEvent>();
// moveEvent->x = (short)LOWORD(lp);
// moveEvent->y = (short)HIWORD(lp);
// native->event_queue.push(moveEvent);
@@ -72,7 +72,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
case WM_CLOSE:
{
- CloseEvent* closeEvent = new CloseEvent();
+ auto closeEvent = std::make_shared<CloseEvent>();
native->event_queue.push(closeEvent);
}
break;
@@ -91,7 +91,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
// return 0;
case WM_MOUSEMOVE:
{
- MouseMoveEvent* mouseMoveEvent = new MouseMoveEvent();
+ auto mouseMoveEvent = std::make_shared<MouseMoveEvent>();
mouseMoveEvent->x = (short)LOWORD(lp);
mouseMoveEvent->y = (short)HIWORD(lp);
native->event_queue.push(mouseMoveEvent);
@@ -100,7 +100,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
case WM_MOUSEWHEEL:
{
- ScrollEvent* scrollEvent = new ScrollEvent();
+ auto scrollEvent = std::make_shared<ScrollEvent>();
// NOTE: lp is coordinates in screen space, not client space.
POINT p;
@@ -125,7 +125,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
case WM_MBUTTONDBLCLK:
case WM_MBUTTONDOWN:
{
- ButtonEvent* buttonEvent = new ButtonEvent();
+ auto buttonEvent = std::make_shared<ButtonEvent>();
buttonEvent->x = (short)LOWORD(lp);
buttonEvent->y = (short)HIWORD(lp);
@@ -178,7 +178,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
case WM_KEYDOWN:
case WM_KEYUP:
{
- KeyEvent* keyEvent = new KeyEvent();
+ auto keyEvent = std::make_shared<KeyEvent>();
switch(wp) {
case VK_LEFT: keyEvent->keycode = Key::left; break;
@@ -207,7 +207,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
{
if(wp >= ' ') // Filter control chars.
{
- KeyEvent* keyEvent = new KeyEvent();
+ auto keyEvent = std::make_shared<KeyEvent>();
keyEvent->keycode = Key::character;
keyEvent->text += (char)wp;
keyEvent->direction = Direction::up;
@@ -218,7 +218,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg,
case WM_PAINT:
{
- RepaintEvent* repaintEvent = new RepaintEvent();
+ auto repaintEvent = std::make_shared<RepaintEvent>();
repaintEvent->x = 0;
repaintEvent->y = 0;
repaintEvent->width = 100;
@@ -417,7 +417,7 @@ bool NativeWindowWin32::hasEvent()
return false;
}
-Event* NativeWindowWin32::getNextEvent()
+std::shared_ptr<Event> NativeWindowWin32::getNextEvent()
{
if(!event_queue.empty())
{
@@ -447,7 +447,7 @@ Event* NativeWindowWin32::getNextEvent()
return event;
}
-Event* NativeWindowWin32::peekNextEvent()
+std::shared_ptr<Event> NativeWindowWin32::peekNextEvent()
{
if(!event_queue.empty())
{
diff --git a/plugingui/nativewindow_win32.h b/plugingui/nativewindow_win32.h
index 3d708b8..110085c 100644
--- a/plugingui/nativewindow_win32.h
+++ b/plugingui/nativewindow_win32.h
@@ -55,8 +55,8 @@ public:
void grabMouse(bool grab) override;
bool hasEvent() override;
- Event* getNextEvent() override;
- Event* peekNextEvent() override;
+ std::shared_ptr<Event> getNextEvent() override;
+ std::shared_ptr<Event> peekNextEvent() override;
private:
static LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
@@ -65,7 +65,7 @@ private:
Window& window;
WNDID m_hwnd = 0;
char* m_className = nullptr;
- std::queue<Event*> event_queue;
+ std::queue<std::shared_ptr<Event>> event_queue;
};
} // GUI::
diff --git a/plugingui/nativewindow_x11.cc b/plugingui/nativewindow_x11.cc
index b0188e1..f50551e 100644
--- a/plugingui/nativewindow_x11.cc
+++ b/plugingui/nativewindow_x11.cc
@@ -354,7 +354,7 @@ bool NativeWindowX11::hasEvent()
return XPending(display);
}
-Event* NativeWindowX11::getNextEvent()
+std::shared_ptr<Event> NativeWindowX11::getNextEvent()
{
if(display == nullptr)
{
@@ -366,7 +366,7 @@ Event* NativeWindowX11::getNextEvent()
return translateXMessage(xEvent);
}
-Event* NativeWindowX11::peekNextEvent()
+std::shared_ptr<Event> NativeWindowX11::peekNextEvent()
{
if(display == nullptr)
{
@@ -378,14 +378,15 @@ Event* NativeWindowX11::peekNextEvent()
return translateXMessage(peekXEvent, true);
}
-Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
+std::shared_ptr<Event> NativeWindowX11::translateXMessage(XEvent& xevent,
+ bool peek)
{
- Event* event = nullptr;
+ std::shared_ptr<Event> event = nullptr;
switch(xevent.type) {
case MotionNotify:
{
- auto mouseMoveEvent = new MouseMoveEvent();
+ auto mouseMoveEvent = std::make_shared<MouseMoveEvent>();
mouseMoveEvent->window_id = xevent.xmotion.window;
mouseMoveEvent->x = xevent.xmotion.x;
mouseMoveEvent->y = xevent.xmotion.y;
@@ -396,7 +397,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
case Expose:
if(xevent.xexpose.count == 0)
{
- auto repaintEvent = new RepaintEvent();
+ auto repaintEvent = std::make_shared<RepaintEvent>();
repaintEvent->window_id = xevent.xexpose.window;
repaintEvent->x = xevent.xexpose.x;
repaintEvent->y = xevent.xexpose.y;
@@ -408,7 +409,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
case ConfigureNotify:
{
- auto resizeEvent = new ResizeEvent();
+ auto resizeEvent = std::make_shared<ResizeEvent>();
resizeEvent->window_id = xevent.xconfigure.window;
//resizeEvent->x = xevent.xconfigure.x;
//resizeEvent->y = xevent.xconfigure.y;
@@ -424,7 +425,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
if((xevent.xbutton.button == 4) || (xevent.xbutton.button == 5))
{
int scroll = 1;
- auto scrollEvent = new ScrollEvent();
+ auto scrollEvent = std::make_shared<ScrollEvent>();
scrollEvent->window_id = xevent.xbutton.window;
scrollEvent->x = xevent.xbutton.x;
scrollEvent->y = xevent.xbutton.y;
@@ -433,7 +434,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
}
else
{
- auto buttonEvent = new ButtonEvent();
+ auto buttonEvent = std::make_shared<ButtonEvent>();
buttonEvent->window_id = xevent.xbutton.window;
buttonEvent->x = xevent.xbutton.x;
buttonEvent->y = xevent.xbutton.y;
@@ -474,7 +475,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
case KeyPress:
case KeyRelease:
{
- auto keyEvent = new KeyEvent();
+ auto keyEvent = std::make_shared<KeyEvent>();
keyEvent->window_id = xevent.xkey.window;
switch(xevent.xkey.keycode) {
@@ -512,7 +513,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek)
case ClientMessage:
if(((unsigned int)xevent.xclient.data.l[0] == wmDeleteMessage))
{
- auto closeEvent = new CloseEvent();
+ auto closeEvent = std::make_shared<CloseEvent>();
event = closeEvent;
}
break;
diff --git a/plugingui/nativewindow_x11.h b/plugingui/nativewindow_x11.h
index 2d99556..50cd904 100644
--- a/plugingui/nativewindow_x11.h
+++ b/plugingui/nativewindow_x11.h
@@ -49,11 +49,11 @@ public:
void redraw() override;
void grabMouse(bool grab) override;
bool hasEvent() override;
- Event* getNextEvent() override;
- Event* peekNextEvent() override;
+ std::shared_ptr<Event> getNextEvent() override;
+ std::shared_ptr<Event> peekNextEvent() override;
private:
- Event* translateXMessage(XEvent& xevent, bool peek = false);
+ std::shared_ptr<Event> translateXMessage(XEvent& xevent, bool peek = false);
XImage* createImageFromBuffer(unsigned char* buf, int width, int height);
::Window xwindow{0};