2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-17 05:15:14 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-02-06 19:03:37 +00:00
|
|
|
#include <WindowServer/Cursor.h>
|
2021-06-19 01:21:30 +00:00
|
|
|
#include <WindowServer/Screen.h>
|
2020-02-06 19:03:37 +00:00
|
|
|
#include <WindowServer/WindowManager.h>
|
2019-03-31 20:09:10 +00:00
|
|
|
|
2020-02-06 19:03:37 +00:00
|
|
|
namespace WindowServer {
|
|
|
|
|
2021-08-02 09:56:05 +00:00
|
|
|
Cursor::Cursor(NonnullRefPtr<Gfx::Bitmap>&& bitmap, int scale_factor, Gfx::CursorParams const& cursor_params)
|
2021-06-19 01:21:30 +00:00
|
|
|
: m_params(cursor_params.constrained(*bitmap))
|
|
|
|
, m_rect(bitmap->rect())
|
2019-03-31 20:09:10 +00:00
|
|
|
{
|
2021-06-19 01:21:30 +00:00
|
|
|
m_bitmaps.set(scale_factor, move(bitmap));
|
2021-06-21 14:02:31 +00:00
|
|
|
update_rect_if_animated();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cursor::update_rect_if_animated()
|
|
|
|
{
|
2020-12-17 05:15:14 +00:00
|
|
|
if (m_params.frames() > 1) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_rect.width() % m_params.frames() == 0);
|
2020-12-17 05:15:14 +00:00
|
|
|
m_rect.set_width(m_rect.width() / m_params.frames());
|
|
|
|
}
|
2019-03-31 20:09:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 01:21:30 +00:00
|
|
|
NonnullRefPtr<Cursor> Cursor::create(NonnullRefPtr<Gfx::Bitmap>&& bitmap, int scale_factor)
|
2019-03-31 20:09:10 +00:00
|
|
|
{
|
2021-06-19 01:21:30 +00:00
|
|
|
auto hotspot = bitmap->rect().center();
|
2021-08-02 09:56:05 +00:00
|
|
|
return adopt_ref(*new Cursor(move(bitmap), scale_factor, Gfx::CursorParams(hotspot)));
|
2019-03-31 20:09:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
RefPtr<Cursor> Cursor::create(StringView filename, StringView default_filename)
|
2019-03-31 20:09:10 +00:00
|
|
|
{
|
2021-06-19 01:21:30 +00:00
|
|
|
auto cursor = adopt_ref(*new Cursor());
|
|
|
|
if (cursor->load(filename, default_filename))
|
|
|
|
return cursor;
|
|
|
|
return {};
|
2019-03-31 20:09:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
bool Cursor::load(StringView filename, StringView default_filename)
|
2019-03-31 20:09:10 +00:00
|
|
|
{
|
2021-06-19 01:21:30 +00:00
|
|
|
bool did_load_any = false;
|
|
|
|
|
2021-11-10 23:55:02 +00:00
|
|
|
auto load_bitmap = [&](StringView path, int scale_factor) {
|
2021-11-06 15:25:29 +00:00
|
|
|
auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(path, scale_factor);
|
|
|
|
if (bitmap_or_error.is_error())
|
|
|
|
return;
|
|
|
|
did_load_any = true;
|
|
|
|
m_bitmaps.set(scale_factor, bitmap_or_error.release_value());
|
2021-06-19 01:21:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Screen::for_each_scale_factor_in_use([&](int scale_factor) {
|
|
|
|
load_bitmap(filename, scale_factor);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
if (!did_load_any) {
|
|
|
|
Screen::for_each_scale_factor_in_use([&](int scale_factor) {
|
|
|
|
load_bitmap(default_filename, scale_factor);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
2021-06-21 14:02:31 +00:00
|
|
|
if (did_load_any) {
|
|
|
|
auto& bitmap = this->bitmap(1);
|
|
|
|
m_rect = bitmap.rect();
|
2021-08-02 09:56:05 +00:00
|
|
|
m_params = Gfx::CursorParams::parse_from_filename(filename, m_rect.center()).constrained(bitmap);
|
2021-06-21 14:02:31 +00:00
|
|
|
update_rect_if_animated();
|
|
|
|
}
|
2021-06-19 01:21:30 +00:00
|
|
|
return did_load_any;
|
2019-03-31 20:09:10 +00:00
|
|
|
}
|
2019-03-31 21:52:02 +00:00
|
|
|
|
2020-09-10 17:25:13 +00:00
|
|
|
RefPtr<Cursor> Cursor::create(Gfx::StandardCursor standard_cursor)
|
2019-03-31 21:52:02 +00:00
|
|
|
{
|
|
|
|
switch (standard_cursor) {
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::None:
|
2019-03-31 21:52:02 +00:00
|
|
|
return nullptr;
|
2020-11-02 06:28:14 +00:00
|
|
|
case Gfx::StandardCursor::Hidden:
|
|
|
|
return WindowManager::the().hidden_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::Arrow:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().arrow_cursor();
|
2020-10-30 17:06:32 +00:00
|
|
|
case Gfx::StandardCursor::Crosshair:
|
|
|
|
return WindowManager::the().crosshair_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::IBeam:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().i_beam_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::ResizeHorizontal:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().resize_horizontally_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::ResizeVertical:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().resize_vertically_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::ResizeDiagonalTLBR:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().resize_diagonally_tlbr_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::ResizeDiagonalBLTR:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().resize_diagonally_bltr_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::ResizeColumn:
|
2020-07-07 18:19:56 +00:00
|
|
|
return WindowManager::the().resize_column_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::ResizeRow:
|
2020-07-07 18:19:56 +00:00
|
|
|
return WindowManager::the().resize_row_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::Hand:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().hand_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::Help:
|
2020-07-07 19:01:29 +00:00
|
|
|
return WindowManager::the().help_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::Drag:
|
2020-02-06 19:03:37 +00:00
|
|
|
return WindowManager::the().drag_cursor();
|
2022-06-20 09:37:22 +00:00
|
|
|
case Gfx::StandardCursor::DragCopy:
|
|
|
|
return WindowManager::the().drag_copy_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::Move:
|
2020-05-12 22:16:40 +00:00
|
|
|
return WindowManager::the().move_cursor();
|
2020-09-10 17:25:13 +00:00
|
|
|
case Gfx::StandardCursor::Wait:
|
2020-07-07 19:23:40 +00:00
|
|
|
return WindowManager::the().wait_cursor();
|
2021-03-11 15:28:56 +00:00
|
|
|
case Gfx::StandardCursor::Disallowed:
|
|
|
|
return WindowManager::the().disallowed_cursor();
|
2021-09-01 02:16:53 +00:00
|
|
|
case Gfx::StandardCursor::Eyedropper:
|
|
|
|
return WindowManager::the().eyedropper_cursor();
|
2021-09-01 02:19:31 +00:00
|
|
|
case Gfx::StandardCursor::Zoom:
|
|
|
|
return WindowManager::the().zoom_cursor();
|
2020-12-25 21:45:47 +00:00
|
|
|
default:
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-03-31 21:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 19:03:37 +00:00
|
|
|
|
|
|
|
}
|