2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <AK/Badge.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-11-07 23:14:21 +03:30
|
|
|
#include <LibCore/MimeData.h>
|
2022-02-25 12:39:33 +02:00
|
|
|
#include <LibGUI/ConnectionToWindowServer.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/DragOperation.h>
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2019-12-08 16:50:23 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-12-08 16:50:23 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
static DragOperation* s_current_drag_operation;
|
|
|
|
|
|
|
|
DragOperation::DragOperation(Core::Object* parent)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Core::Object(parent)
|
2019-12-08 16:50:23 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
DragOperation::Outcome DragOperation::exec()
|
2019-12-08 16:50:23 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!s_current_drag_operation);
|
|
|
|
VERIFY(!m_event_loop);
|
|
|
|
VERIFY(m_mime_data);
|
2019-12-08 16:50:23 +01:00
|
|
|
|
2021-01-16 10:06:27 +01:00
|
|
|
Gfx::ShareableBitmap drag_bitmap;
|
2020-11-07 23:14:21 +03:30
|
|
|
if (m_mime_data->has_format("image/x-raw-bitmap")) {
|
|
|
|
auto data = m_mime_data->data("image/x-raw-bitmap");
|
2023-01-20 20:06:05 +01:00
|
|
|
auto bitmap = Gfx::Bitmap::create_from_serialized_byte_buffer(move(data)).release_value_but_fixme_should_propagate_errors();
|
2021-01-16 10:06:27 +01:00
|
|
|
drag_bitmap = bitmap->to_shareable_bitmap();
|
2019-12-08 17:08:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 12:39:33 +02:00
|
|
|
auto started = ConnectionToWindowServer::the().start_drag(
|
2020-11-07 23:14:21 +03:30
|
|
|
m_mime_data->text(),
|
|
|
|
m_mime_data->all_data(),
|
2021-01-16 10:06:27 +01:00
|
|
|
drag_bitmap);
|
2020-11-07 23:14:21 +03:30
|
|
|
|
2021-05-03 13:55:29 +02:00
|
|
|
if (!started) {
|
2019-12-08 16:50:23 +01:00
|
|
|
m_outcome = Outcome::Cancelled;
|
|
|
|
return m_outcome;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_current_drag_operation = this;
|
2020-02-02 12:34:39 +01:00
|
|
|
m_event_loop = make<Core::EventLoop>();
|
2019-12-08 16:50:23 +01:00
|
|
|
auto result = m_event_loop->exec();
|
|
|
|
m_event_loop = nullptr;
|
2022-08-12 19:33:05 -04:00
|
|
|
dbgln_if(DRAG_DEBUG, "{}: event loop returned with result {}", class_name(), result);
|
2019-12-08 16:50:23 +01:00
|
|
|
remove_from_parent();
|
|
|
|
s_current_drag_operation = nullptr;
|
|
|
|
return m_outcome;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void DragOperation::done(Outcome outcome)
|
2019-12-08 16:50:23 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_outcome == Outcome::None);
|
2019-12-08 16:50:23 +01:00
|
|
|
m_outcome = outcome;
|
|
|
|
m_event_loop->quit(0);
|
|
|
|
}
|
|
|
|
|
2022-02-25 12:39:33 +02:00
|
|
|
void DragOperation::notify_accepted(Badge<ConnectionToWindowServer>)
|
2019-12-08 16:50:23 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(s_current_drag_operation);
|
2019-12-08 16:50:23 +01:00
|
|
|
s_current_drag_operation->done(Outcome::Accepted);
|
|
|
|
}
|
|
|
|
|
2022-02-25 12:39:33 +02:00
|
|
|
void DragOperation::notify_cancelled(Badge<ConnectionToWindowServer>)
|
2019-12-08 16:50:23 +01:00
|
|
|
{
|
2021-01-08 23:47:53 +01:00
|
|
|
if (s_current_drag_operation)
|
|
|
|
s_current_drag_operation->done(Outcome::Cancelled);
|
2019-12-08 16:50:23 +01:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void DragOperation::set_text(DeprecatedString const& text)
|
2020-11-07 23:14:21 +03:30
|
|
|
{
|
|
|
|
if (!m_mime_data)
|
|
|
|
m_mime_data = Core::MimeData::construct();
|
|
|
|
m_mime_data->set_text(text);
|
|
|
|
}
|
2022-04-01 20:58:27 +03:00
|
|
|
void DragOperation::set_bitmap(Gfx::Bitmap const* bitmap)
|
2020-11-07 23:14:21 +03:30
|
|
|
{
|
|
|
|
if (!m_mime_data)
|
|
|
|
m_mime_data = Core::MimeData::construct();
|
|
|
|
if (bitmap)
|
2023-02-08 18:21:41 +01:00
|
|
|
m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer().release_value_but_fixme_should_propagate_errors());
|
2020-11-07 23:14:21 +03:30
|
|
|
}
|
2022-12-04 18:02:33 +00:00
|
|
|
void DragOperation::set_data(DeprecatedString const& data_type, DeprecatedString const& data)
|
2020-11-07 23:14:21 +03:30
|
|
|
{
|
|
|
|
if (!m_mime_data)
|
|
|
|
m_mime_data = Core::MimeData::construct();
|
|
|
|
m_mime_data->set_data(data_type, data.to_byte_buffer());
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
}
|