2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/DragOperation.h>
|
|
|
|
#include <LibGUI/WindowServerConnection.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::~DragOperation()
|
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");
|
|
|
|
auto bitmap = Gfx::Bitmap::create_from_serialized_byte_buffer(move(data));
|
2021-01-16 10:06:27 +01:00
|
|
|
drag_bitmap = bitmap->to_shareable_bitmap();
|
2019-12-08 17:08:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 13:33:59 +02:00
|
|
|
auto response = WindowServerConnection::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:33:59 +02:00
|
|
|
if (!response.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;
|
2021-01-10 10:02:20 +01:00
|
|
|
dbgln("{}: 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);
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void DragOperation::notify_accepted(Badge<WindowServerConnection>)
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
|
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
|
|
|
|
2020-11-07 23:14:21 +03:30
|
|
|
void DragOperation::set_text(const String& text)
|
|
|
|
{
|
|
|
|
if (!m_mime_data)
|
|
|
|
m_mime_data = Core::MimeData::construct();
|
|
|
|
m_mime_data->set_text(text);
|
|
|
|
}
|
|
|
|
void DragOperation::set_bitmap(const Gfx::Bitmap* bitmap)
|
|
|
|
{
|
|
|
|
if (!m_mime_data)
|
|
|
|
m_mime_data = Core::MimeData::construct();
|
|
|
|
if (bitmap)
|
|
|
|
m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
|
|
|
|
}
|
|
|
|
void DragOperation::set_data(const String& data_type, const String& data)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|