ladybird/Libraries/LibGUI/Event.h

387 lines
11 KiB
C
Raw Normal View History

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2018-10-10 16:12:38 +03:00
#pragma once
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <Kernel/API/KeyCode.h>
#include <LibCore/Event.h>
2020-03-16 14:36:21 +03:00
#include <LibGUI/WindowType.h>
2020-02-06 14:04:00 +03:00
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
2018-10-10 16:12:38 +03:00
namespace GUI {
class Event : public Core::Event {
2018-10-10 16:12:38 +03:00
public:
enum Type {
Show = 1000,
2018-10-10 16:12:38 +03:00
Hide,
Paint,
MultiPaint,
Resize,
2018-10-10 16:12:38 +03:00
MouseMove,
MouseDown,
MouseDoubleClick,
2018-10-10 16:12:38 +03:00
MouseUp,
MouseWheel,
Enter,
Leave,
2018-10-10 16:12:38 +03:00
KeyDown,
KeyUp,
WindowEntered,
WindowLeft,
WindowBecameInactive,
WindowBecameActive,
WindowInputEntered,
WindowInputLeft,
FocusIn,
FocusOut,
WindowCloseRequest,
ContextMenu,
EnabledChange,
DragMove,
Drop,
2020-03-16 14:36:21 +03:00
ThemeChange,
__Begin_WM_Events,
2019-04-03 22:03:12 +03:00
WM_WindowRemoved,
WM_WindowStateChanged,
WM_WindowRectChanged,
WM_WindowIconBitmapChanged,
__End_WM_Events,
2018-10-10 16:12:38 +03:00
};
Event() {}
explicit Event(Type type)
: Core::Event(type)
{
}
virtual ~Event() {}
2018-10-10 16:12:38 +03:00
bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
bool is_paint_event() const { return type() == Paint; }
};
class WMEvent : public Event {
2019-04-03 22:03:12 +03:00
public:
WMEvent(Type type, int client_id, int window_id)
: Event(type)
2019-04-03 22:03:12 +03:00
, m_client_id(client_id)
, m_window_id(window_id)
{
}
int client_id() const { return m_client_id; }
int window_id() const { return m_window_id; }
private:
int m_client_id { -1 };
int m_window_id { -1 };
};
class WMWindowRemovedEvent : public WMEvent {
2019-04-03 22:03:12 +03:00
public:
WMWindowRemovedEvent(int client_id, int window_id)
: WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
2019-04-03 22:03:12 +03:00
{
}
};
class WMWindowStateChangedEvent : public WMEvent {
2019-04-03 22:03:12 +03:00
public:
WMWindowStateChangedEvent(int client_id, int window_id, int parent_client_id, int parent_window_id, const StringView& title, const Gfx::IntRect& rect, bool is_active, bool is_modal, WindowType window_type, bool is_minimized, bool is_frameless, int progress)
: WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
, m_parent_client_id(parent_client_id)
, m_parent_window_id(parent_window_id)
2019-04-03 22:03:12 +03:00
, m_title(title)
, m_rect(rect)
, m_window_type(window_type)
, m_active(is_active)
, m_modal(is_modal)
, m_minimized(is_minimized)
, m_frameless(is_frameless)
, m_progress(progress)
2019-04-03 22:03:12 +03:00
{
}
int parent_client_id() const { return m_parent_client_id; }
int parent_window_id() const { return m_parent_window_id; }
2019-04-03 22:03:12 +03:00
String title() const { return m_title; }
Gfx::IntRect rect() const { return m_rect; }
bool is_active() const { return m_active; }
bool is_modal() const { return m_modal; }
WindowType window_type() const { return m_window_type; }
bool is_minimized() const { return m_minimized; }
bool is_frameless() const { return m_frameless; }
int progress() const { return m_progress; }
2019-04-03 22:03:12 +03:00
private:
int m_parent_client_id;
int m_parent_window_id;
2019-04-03 22:03:12 +03:00
String m_title;
Gfx::IntRect m_rect;
WindowType m_window_type;
bool m_active;
bool m_modal;
bool m_minimized;
bool m_frameless;
int m_progress;
2019-04-03 22:03:12 +03:00
};
class WMWindowRectChangedEvent : public WMEvent {
public:
WMWindowRectChangedEvent(int client_id, int window_id, const Gfx::IntRect& rect)
: WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
, m_rect(rect)
{
}
Gfx::IntRect rect() const { return m_rect; }
private:
Gfx::IntRect m_rect;
};
class WMWindowIconBitmapChangedEvent : public WMEvent {
public:
WMWindowIconBitmapChangedEvent(int client_id, int window_id, int icon_buffer_id, const Gfx::IntSize& icon_size)
: WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
, m_icon_buffer_id(icon_buffer_id)
, m_icon_size(icon_size)
{
}
int icon_buffer_id() const { return m_icon_buffer_id; }
const Gfx::IntSize& icon_size() const { return m_icon_size; }
private:
int m_icon_buffer_id;
Gfx::IntSize m_icon_size;
};
class MultiPaintEvent final : public Event {
public:
explicit MultiPaintEvent(const Vector<Gfx::IntRect, 32>& rects, const Gfx::IntSize& window_size)
: Event(Event::MultiPaint)
, m_rects(rects)
, m_window_size(window_size)
{
}
const Vector<Gfx::IntRect, 32>& rects() const { return m_rects; }
Gfx::IntSize window_size() const { return m_window_size; }
private:
Vector<Gfx::IntRect, 32> m_rects;
Gfx::IntSize m_window_size;
};
class PaintEvent final : public Event {
2018-10-10 16:12:38 +03:00
public:
explicit PaintEvent(const Gfx::IntRect& rect, const Gfx::IntSize& window_size = {})
: Event(Event::Paint)
, m_rect(rect)
, m_window_size(window_size)
2018-10-10 16:12:38 +03:00
{
}
Gfx::IntRect rect() const { return m_rect; }
Gfx::IntSize window_size() const { return m_window_size; }
private:
Gfx::IntRect m_rect;
Gfx::IntSize m_window_size;
2018-10-10 16:12:38 +03:00
};
class ResizeEvent final : public Event {
public:
explicit ResizeEvent(const Gfx::IntSize& old_size, const Gfx::IntSize& size)
: Event(Event::Resize)
, m_old_size(old_size)
, m_size(size)
{
}
const Gfx::IntSize& old_size() const { return m_old_size; }
const Gfx::IntSize& size() const { return m_size; }
private:
Gfx::IntSize m_old_size;
Gfx::IntSize m_size;
};
class ContextMenuEvent final : public Event {
public:
explicit ContextMenuEvent(const Gfx::IntPoint& position, const Gfx::IntPoint& screen_position)
: Event(Event::ContextMenu)
, m_position(position)
, m_screen_position(screen_position)
{
}
const Gfx::IntPoint& position() const { return m_position; }
const Gfx::IntPoint& screen_position() const { return m_screen_position; }
private:
Gfx::IntPoint m_position;
Gfx::IntPoint m_screen_position;
};
class ShowEvent final : public Event {
2018-10-10 16:12:38 +03:00
public:
ShowEvent()
: Event(Event::Show)
2018-10-10 16:12:38 +03:00
{
}
};
class HideEvent final : public Event {
2018-10-10 16:12:38 +03:00
public:
HideEvent()
: Event(Event::Hide)
2018-10-10 16:12:38 +03:00
{
}
};
enum MouseButton : u8 {
2018-10-10 16:12:38 +03:00
None = 0,
Left = 1,
Right = 2,
Middle = 4,
Back = 8,
Forward = 16,
2018-10-10 16:12:38 +03:00
};
class KeyEvent final : public Event {
2018-10-10 16:12:38 +03:00
public:
KeyEvent(Type type, KeyCode key, u8 modifiers, u32 code_point, u32 scancode)
: Event(type)
2018-10-10 16:12:38 +03:00
, m_key(key)
, m_modifiers(modifiers)
, m_code_point(code_point)
2020-05-30 15:53:06 +03:00
, m_scancode(scancode)
2018-10-10 16:12:38 +03:00
{
}
KeyCode key() const { return m_key; }
bool ctrl() const { return m_modifiers & Mod_Ctrl; }
bool alt() const { return m_modifiers & Mod_Alt; }
bool shift() const { return m_modifiers & Mod_Shift; }
bool logo() const { return m_modifiers & Mod_Logo; }
u8 modifiers() const { return m_modifiers; }
u32 code_point() const { return m_code_point; }
String text() const
{
StringBuilder sb;
sb.append_codepoint(m_code_point);
return sb.to_string();
}
2020-05-30 15:53:06 +03:00
u32 scancode() const { return m_scancode; }
2018-10-10 16:12:38 +03:00
String to_string() const;
2018-10-10 16:12:38 +03:00
private:
friend class WindowServerConnection;
KeyCode m_key { KeyCode::Key_Invalid };
u8 m_modifiers { 0 };
u32 m_code_point { 0 };
2020-05-30 15:53:06 +03:00
u32 m_scancode { 0 };
String m_text2;
2018-10-10 16:12:38 +03:00
};
class MouseEvent final : public Event {
2018-10-10 16:12:38 +03:00
public:
MouseEvent(Type type, const Gfx::IntPoint& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta)
: Event(type)
, m_position(position)
, m_buttons(buttons)
2018-10-10 16:12:38 +03:00
, m_button(button)
, m_modifiers(modifiers)
, m_wheel_delta(wheel_delta)
2018-10-10 16:12:38 +03:00
{
}
Gfx::IntPoint position() const { return m_position; }
2018-10-12 02:20:06 +03:00
int x() const { return m_position.x(); }
int y() const { return m_position.y(); }
MouseButton button() const { return m_button; }
unsigned buttons() const { return m_buttons; }
unsigned modifiers() const { return m_modifiers; }
int wheel_delta() const { return m_wheel_delta; }
2018-10-10 16:12:38 +03:00
private:
Gfx::IntPoint m_position;
unsigned m_buttons { 0 };
MouseButton m_button { MouseButton::None };
unsigned m_modifiers { 0 };
int m_wheel_delta { 0 };
2018-10-10 16:12:38 +03:00
};
class DragEvent final : public Event {
public:
DragEvent(Type type, const Gfx::IntPoint& position, const String& data_type)
: Event(type)
, m_position(position)
, m_data_type(data_type)
{
}
const Gfx::IntPoint& position() const { return m_position; }
const String& data_type() const { return m_data_type; }
private:
Gfx::IntPoint m_position;
String m_data_type;
};
class DropEvent final : public Event {
public:
DropEvent(const Gfx::IntPoint&, const String& text, NonnullRefPtr<Core::MimeData> mime_data);
~DropEvent();
const Gfx::IntPoint& position() const { return m_position; }
const String& text() const { return m_text; }
const Core::MimeData& mime_data() const { return m_mime_data; }
private:
Gfx::IntPoint m_position;
String m_text;
NonnullRefPtr<Core::MimeData> m_mime_data;
};
2020-03-16 14:36:21 +03:00
class ThemeChangeEvent final : public Event {
public:
ThemeChangeEvent()
: Event(Type::ThemeChange)
{
}
};
}