From 68b17f40c011b458cfb01ebe78a60d4784498f9c Mon Sep 17 00:00:00 2001 From: Andreas Butti Date: Sat, 15 Dec 2018 10:22:25 +0100 Subject: [PATCH] Input tool selection --- src/gui/inputdevices/AbstractInputDevice.cpp | 18 ++-- src/gui/inputdevices/AbstractInputDevice.h | 4 +- src/gui/inputdevices/InputSequence.cpp | 91 +++++++++++++++++++- src/gui/inputdevices/InputSequence.h | 9 +- src/gui/inputdevices/NewGtkInputDevice.cpp | 22 ++++- src/gui/inputdevices/NewGtkInputDevice.h | 13 ++- 6 files changed, 136 insertions(+), 21 deletions(-) diff --git a/src/gui/inputdevices/AbstractInputDevice.cpp b/src/gui/inputdevices/AbstractInputDevice.cpp index 3dfb4458..087ff3c3 100644 --- a/src/gui/inputdevices/AbstractInputDevice.cpp +++ b/src/gui/inputdevices/AbstractInputDevice.cpp @@ -93,7 +93,7 @@ bool AbstractInputDevice::handleButtonPress(GdkEventButton* event) ToolHandler* h = xournal->view->getControl()->getToolHandler(); // Change the tool depending on the key or device - if (changeTool(event)) + if (changeTool(event->device, event->button)) { return true; } @@ -134,7 +134,8 @@ bool AbstractInputDevice::handleButtonPress(GdkEventButton* event) else { xournal->view->clearSelection(); - if (changeTool(event)) + + if (changeTool(event->device, event->button)) { return true; } @@ -269,33 +270,30 @@ bool AbstractInputDevice::handleMotion(GdkEventMotion* event) return false; } -bool AbstractInputDevice::changeTool(GdkEventButton* event) +bool AbstractInputDevice::changeTool(GdkDevice* device, int button) { Settings* settings = view->getControl()->getSettings(); ButtonConfig* cfgTouch = settings->getTouchButtonConfig(); ToolHandler* h = view->getControl()->getToolHandler(); - GdkEvent* rawEvent = (GdkEvent*) event; - GdkDevice* device = gdk_event_get_source_device(rawEvent); - GtkXournal* xournal = GTK_XOURNAL(widget); ButtonConfig* cfg = NULL; if (gdk_device_get_source(device) == GDK_SOURCE_PEN) { - if (event->button == 2) + if (button == 2) { cfg = settings->getStylusButton1Config(); } - else if (event->button == 3) + else if (button == 3) { cfg = settings->getStylusButton2Config(); } } - else if (event->button == 2) // Middle Button + else if (button == 2) // Middle Button { cfg = settings->getMiddleButtonConfig(); } - else if (event->button == 3 && !xournal->selection) // Right Button + else if (button == 3 && !xournal->selection) // Right Button { cfg = settings->getRightButtonConfig(); } diff --git a/src/gui/inputdevices/AbstractInputDevice.h b/src/gui/inputdevices/AbstractInputDevice.h index eff4b2a0..b5c90e91 100644 --- a/src/gui/inputdevices/AbstractInputDevice.h +++ b/src/gui/inputdevices/AbstractInputDevice.h @@ -42,7 +42,9 @@ protected: bool handleButtonPress(GdkEventButton* event); bool handleButtonRelease(GdkEventButton* event); bool handleMotion(GdkEventMotion* event); - bool changeTool(GdkEventButton* event); + +public: + bool changeTool(GdkDevice* device, int button); private: XOJ_TYPE_ATTRIB; diff --git a/src/gui/inputdevices/InputSequence.cpp b/src/gui/inputdevices/InputSequence.cpp index 2596adac..2360274d 100644 --- a/src/gui/inputdevices/InputSequence.cpp +++ b/src/gui/inputdevices/InputSequence.cpp @@ -1,7 +1,10 @@ #include "InputSequence.h" +#include "NewGtkInputDevice.h" -InputSequence::InputSequence() - : device(NULL), + +InputSequence::InputSequence(NewGtkInputDevice* inputHandler) + : inputHandler(inputHandler), + device(NULL), axes(NULL), x(-1), y(-1) @@ -80,6 +83,8 @@ void InputSequence::actionMoved() { XOJ_CHECK_TYPE(InputSequence); + inputHandler->changeTool(device, 0); + printf("actionMoved %s\n", gdk_device_get_name(device)); } @@ -90,9 +95,91 @@ void InputSequence::actionStart() { XOJ_CHECK_TYPE(InputSequence); + inputHandler->focusWidget(); + printf("actionStart %s\n", gdk_device_get_name(device)); } +/* + + + // none button release event was sent, send one now + if (xournal->currentInputPage) + { + GdkEventButton ev = *event; + xournal->currentInputPage->translateEvent((GdkEvent*) &ev, xournal->x, xournal->y); + xournal->currentInputPage->onButtonReleaseEvent(widget, &ev); + } + + ToolHandler* h = xournal->view->getControl()->getToolHandler(); + + // Change the tool depending on the key or device + if (changeTool(event)) + { + return true; + } + + // hand tool don't change the selection, so you can scroll e.g. + // with your touchscreen without remove the selection + if (h->getToolType() == TOOL_HAND) + { + Cursor* cursor = xournal->view->getCursor(); + cursor->setMouseDown(true); + xournal->inScrolling = true; + //set reference + xournal->lastMousePositionX = event->x_root; + xournal->lastMousePositionY = event->y_root; + + return TRUE; + } + else if (xournal->selection) + { + EditSelection* selection = xournal->selection; + + XojPageView* view = selection->getView(); + GdkEventButton ev = *event; + view->translateEvent((GdkEvent*) &ev, xournal->x, xournal->y); + CursorSelectionType selType = selection->getSelectionTypeForPos(ev.x, ev.y, xournal->view->getZoom()); + if (selType) + { + + if (selType == CURSOR_SELECTION_MOVE && event->button == 3) + { + selection->copySelection(); + } + + xournal->view->getCursor()->setMouseDown(true); + xournal->selection->mouseDown(selType, ev.x, ev.y); + return true; + } + else + { + xournal->view->clearSelection(); + if (changeTool(event)) + { + return true; + } + } + } + + XojPageView* pv = gtk_xournal_get_page_view_for_pos_cached(xournal, event->x, event->y); + + current_view = pv; + + if (pv) + { + xournal->currentInputPage = pv; + pv->translateEvent((GdkEvent*) event, xournal->x, xournal->y); + + xournal->view->getDocument()->indexOf(pv->getPage()); + return pv->onButtonPressEvent(widget, event); + } + + return FALSE; // not handled + + + */ + /** * Mouse / Pen up / touch end */ diff --git a/src/gui/inputdevices/InputSequence.h b/src/gui/inputdevices/InputSequence.h index 761a2408..ab3b99a7 100644 --- a/src/gui/inputdevices/InputSequence.h +++ b/src/gui/inputdevices/InputSequence.h @@ -16,10 +16,12 @@ #include +class NewGtkInputDevice; + class InputSequence { public: - InputSequence(); + InputSequence(NewGtkInputDevice* inputHandler); virtual ~InputSequence(); public: @@ -76,6 +78,11 @@ public: private: XOJ_TYPE_ATTRIB; + /** + * Input Handler + */ + NewGtkInputDevice* inputHandler; + /** * Current input device */ diff --git a/src/gui/inputdevices/NewGtkInputDevice.cpp b/src/gui/inputdevices/NewGtkInputDevice.cpp index 59fdae0f..8b1a92a3 100644 --- a/src/gui/inputdevices/NewGtkInputDevice.cpp +++ b/src/gui/inputdevices/NewGtkInputDevice.cpp @@ -28,6 +28,16 @@ NewGtkInputDevice::~NewGtkInputDevice() XOJ_RELEASE_TYPE(NewGtkInputDevice); } +/** + * Focus the widget + */ +void NewGtkInputDevice::focusWidget() +{ + XOJ_CHECK_TYPE(NewGtkInputDevice); + + gtk_widget_grab_focus(widget); +} + /** * Initialize the input handling, set input events */ @@ -50,7 +60,6 @@ void NewGtkInputDevice::initWidget() GDK_TOUCH_MASK); g_signal_connect(widget, "event", G_CALLBACK(event_cb), this); - g_signal_connect(widget, "touch-event", G_CALLBACK(touch_event_cb), this); } bool NewGtkInputDevice::event_cb(GtkWidget* widget, GdkEvent* event, NewGtkInputDevice* self) @@ -94,7 +103,7 @@ bool NewGtkInputDevice::eventHandler(GdkEvent* event) if (input == NULL) { - input = new InputSequence(); + input = new InputSequence(this); g_hash_table_insert(pointerInputList, device, input); } } @@ -104,7 +113,7 @@ bool NewGtkInputDevice::eventHandler(GdkEvent* event) if (input == NULL) { - input = new InputSequence(); + input = new InputSequence(this); g_hash_table_insert(touchInputList, sequence, input); } } @@ -142,6 +151,13 @@ bool NewGtkInputDevice::eventHandler(GdkEvent* event) } else if (event->type == GDK_BUTTON_PRESS) { + guint button = 0; + // scroll wheel events + if (gdk_event_get_button(event, &button) && button > 3) + { + return FALSE; + } + input->copyAxes(event); input->actionStart(); } diff --git a/src/gui/inputdevices/NewGtkInputDevice.h b/src/gui/inputdevices/NewGtkInputDevice.h index 63c2e283..b88d27a1 100644 --- a/src/gui/inputdevices/NewGtkInputDevice.h +++ b/src/gui/inputdevices/NewGtkInputDevice.h @@ -24,18 +24,23 @@ public: /** * Initialize the input handling, set input events */ - virtual void initWidget(); + void initWidget(); + + /** + * Focus the widget + */ + void focusWidget(); protected: /** * Mouse / pen moved event */ - virtual bool motionEvent(XojPageView* pageView, GdkEventMotion* event); + bool motionEvent(XojPageView* pageView, GdkEventMotion* event); /** * Handle all GTK Events */ - virtual bool eventHandler(GdkEvent* event); + bool eventHandler(GdkEvent* event); protected: /** @@ -46,7 +51,7 @@ protected: /** * Read pressure and position of the pen, if a pen is active */ - virtual void readPositionAndPressure(GdkEventMotion* event, double& x, double& y, double& pressure); + void readPositionAndPressure(GdkEventMotion* event, double& x, double& y, double& pressure); private: static bool event_cb(GtkWidget* widget, GdkEvent* event, NewGtkInputDevice* self);