2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief User state manager
|
|
|
|
* \date 2005-11-16
|
|
|
|
* \author Norman Feske <norman.feske@genode-labs.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2005-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-30 00:21:53 +00:00
|
|
|
#ifndef _INCLUDE__SCOUT__USER_STATE_H_
|
|
|
|
#define _INCLUDE__SCOUT__USER_STATE_H_
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-12-30 00:21:53 +00:00
|
|
|
#include <scout/window.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-12-30 00:21:53 +00:00
|
|
|
namespace Scout { class User_state; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-12-30 00:21:53 +00:00
|
|
|
|
|
|
|
class Scout::User_state : public Parent_element
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
/*
|
|
|
|
* Noncopyable
|
|
|
|
*/
|
|
|
|
User_state(User_state const &);
|
|
|
|
User_state &operator = (User_state const &);
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
Element *_mfocus; /* element that owns the current mouse focus */
|
|
|
|
Element *_active; /* currently activated element */
|
2013-12-30 00:21:53 +00:00
|
|
|
Window *_window;
|
|
|
|
Element *_root; /* root of element tree */
|
2011-12-22 15:19:25 +00:00
|
|
|
int _key_cnt; /* number of currently pressed keys */
|
2013-12-30 00:21:53 +00:00
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
Point _mouse_position { };
|
|
|
|
Point _view_position { };
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign new mouse focus element
|
|
|
|
*/
|
|
|
|
void _assign_mfocus(Element *e, int force = 0)
|
|
|
|
{
|
|
|
|
/* return if mouse focus did not change */
|
|
|
|
if (!force && e == _mfocus) return;
|
|
|
|
|
|
|
|
/* tell old mouse focus to release focus */
|
|
|
|
if (_mfocus) _mfocus->mfocus(0);
|
|
|
|
|
|
|
|
/* assign new current mouse focus */
|
|
|
|
_mfocus = e;
|
|
|
|
|
|
|
|
/* notify new mouse focus */
|
|
|
|
if (_mfocus) _mfocus->mfocus(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
User_state(Window *window, Element *root, int vx, int vy)
|
2013-12-30 00:21:53 +00:00
|
|
|
:
|
|
|
|
_mfocus(0), _active(0), _window(window), _root(root),
|
|
|
|
_key_cnt(0), _view_position(Point(vx, vy))
|
|
|
|
{ }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Accessor functions
|
|
|
|
*/
|
2013-12-30 00:21:53 +00:00
|
|
|
Point mouse_position() const { return _mouse_position; }
|
|
|
|
Point view_position() const { return _view_position; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-05-22 17:21:29 +00:00
|
|
|
/**
|
|
|
|
* Update the current view offset
|
|
|
|
*/
|
|
|
|
void update_view_offset()
|
|
|
|
{
|
2013-12-30 00:21:53 +00:00
|
|
|
_view_position = Point(_window->view_x(), _window->view_y());
|
2013-05-22 17:21:29 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
/**
|
|
|
|
* Apply input event to mouse focus state
|
|
|
|
*/
|
2016-11-23 16:07:49 +00:00
|
|
|
void handle_event(Event const &ev)
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
|
|
|
_key_cnt += ev.type == Event::PRESS ? 1 : 0;
|
|
|
|
_key_cnt -= ev.type == Event::RELEASE ? 1 : 0;
|
|
|
|
|
|
|
|
if (_key_cnt < 0) _key_cnt = 0;
|
|
|
|
|
|
|
|
if (_active)
|
|
|
|
_active->handle_event(ev);
|
|
|
|
|
|
|
|
/* find element under the mouse cursor */
|
2013-12-30 00:21:53 +00:00
|
|
|
_mouse_position = ev.mouse_position;
|
|
|
|
|
|
|
|
Element *e = _root->find(_mouse_position);
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
switch (ev.type) {
|
|
|
|
|
|
|
|
case Event::PRESS:
|
|
|
|
|
|
|
|
if (_key_cnt != 1) break;
|
|
|
|
if (!e) break;
|
|
|
|
|
|
|
|
_active = e;
|
|
|
|
_active->handle_event(ev);
|
|
|
|
|
2013-05-22 17:21:29 +00:00
|
|
|
update_view_offset();
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2013-12-30 00:21:53 +00:00
|
|
|
_assign_mfocus(_root->find(ev.mouse_position), 1);
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Event::RELEASE:
|
|
|
|
|
|
|
|
if (_key_cnt == 0) {
|
2013-05-22 17:21:29 +00:00
|
|
|
update_view_offset();
|
2011-12-22 15:19:25 +00:00
|
|
|
_active = 0;
|
|
|
|
_assign_mfocus(e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Event::MOTION:
|
|
|
|
|
|
|
|
if (!_active && e) e->handle_event(ev);
|
|
|
|
if (_key_cnt == 0)
|
|
|
|
_assign_mfocus(e);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Event::WHEEL:
|
|
|
|
|
|
|
|
if (_key_cnt == 0)
|
Make util/geometry.h C++20 friendly
- Move header to base/include to make it applicable for base types
like 'Affinity' down the road.
- Represent 'Rect' as typle of point and area, which is the most
common form of initialization, creates in valid 'Rect' by default.
- Turn Point, Area, and Rect into compound types, making x, y, w, h, at,
area accessible without a method call
- 'Rect::Compound' function for constructing a 'Rect' from two points,
replacing a former constructor
- Use result type 'Rect::Cut_remainder' instead of out parameters.
Fixes #5239
2024-06-05 10:40:13 +00:00
|
|
|
_window->ypos(_window->ypos() + 23 * ev.mouse_position.y);
|
2011-12-22 15:19:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************
|
|
|
|
** Parent element **
|
|
|
|
********************/
|
|
|
|
|
2019-02-14 21:39:08 +00:00
|
|
|
void forget(Element const *e) override
|
2011-12-22 15:19:25 +00:00
|
|
|
{
|
|
|
|
if (_mfocus == e) _mfocus = 0;
|
|
|
|
if (_active == e) _active = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-30 00:21:53 +00:00
|
|
|
#endif /* _INCLUDE__SCOUT__USER_STATE_H_ */
|