2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-22 11:08:39 +00:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2020-02-16 08:17:49 +00:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Application.h>
|
2020-05-14 20:51:15 +00:00
|
|
|
#include <LibGUI/Clipboard.h>
|
2020-02-06 19:33:02 +00:00
|
|
|
#include <LibGUI/Desktop.h>
|
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
#include <LibGUI/MenuBar.h>
|
|
|
|
#include <LibGUI/Painter.h>
|
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
#include <LibGUI/WindowServerConnection.h>
|
2020-02-14 22:53:11 +00:00
|
|
|
#include <LibGfx/Font.h>
|
|
|
|
#include <LibGfx/Palette.h>
|
2019-02-11 13:43:43 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
namespace GUI {
|
2019-02-11 13:56:23 +00:00
|
|
|
|
2020-08-22 11:08:39 +00:00
|
|
|
static NeverDestroyed<WeakPtr<Application>> s_the;
|
2020-02-02 14:07:41 +00:00
|
|
|
|
2020-07-04 14:52:01 +00:00
|
|
|
Application* Application::the()
|
2019-02-11 13:56:23 +00:00
|
|
|
{
|
2020-08-22 11:08:39 +00:00
|
|
|
return *s_the;
|
2019-02-11 13:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
Application::Application(int argc, char** argv)
|
2019-02-11 13:43:43 +00:00
|
|
|
{
|
2020-08-22 11:08:39 +00:00
|
|
|
ASSERT(!*s_the);
|
|
|
|
*s_the = make_weak_ptr();
|
2020-02-02 11:34:39 +00:00
|
|
|
m_event_loop = make<Core::EventLoop>();
|
2020-02-02 14:07:41 +00:00
|
|
|
WindowServerConnection::the();
|
2020-05-14 20:51:15 +00:00
|
|
|
Clipboard::initialize({});
|
2019-10-06 11:38:19 +00:00
|
|
|
if (argc > 0)
|
|
|
|
m_invoked_as = argv[0];
|
2020-05-12 13:47:13 +00:00
|
|
|
|
2020-08-14 17:55:25 +00:00
|
|
|
if (getenv("GUI_FOCUS_DEBUG"))
|
|
|
|
m_focus_debugging_enabled = true;
|
2020-05-12 13:47:13 +00:00
|
|
|
|
2020-08-14 17:55:25 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
String arg(argv[i]);
|
2020-05-12 13:47:13 +00:00
|
|
|
m_args.append(move(arg));
|
|
|
|
}
|
2019-02-11 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
Application::~Application()
|
2019-02-11 13:43:43 +00:00
|
|
|
{
|
2020-07-04 14:52:01 +00:00
|
|
|
revoke_weak_ptrs();
|
2019-02-11 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
int Application::exec()
|
2019-02-11 13:43:43 +00:00
|
|
|
{
|
2020-07-01 18:49:00 +00:00
|
|
|
return m_event_loop->exec();
|
2019-02-11 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::quit(int exit_code)
|
2019-02-11 13:56:23 +00:00
|
|
|
{
|
2019-02-17 08:58:35 +00:00
|
|
|
m_event_loop->quit(exit_code);
|
2019-02-11 13:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 14:01:00 +00:00
|
|
|
void Application::set_menubar(RefPtr<MenuBar> menubar)
|
2019-02-11 13:43:43 +00:00
|
|
|
{
|
2019-02-11 14:37:12 +00:00
|
|
|
if (m_menubar)
|
2019-05-31 13:44:04 +00:00
|
|
|
m_menubar->notify_removed_from_application({});
|
2019-02-11 13:43:43 +00:00
|
|
|
m_menubar = move(menubar);
|
2019-02-11 14:37:12 +00:00
|
|
|
if (m_menubar)
|
2019-05-31 13:44:04 +00:00
|
|
|
m_menubar->notify_added_to_application({});
|
2019-02-11 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::register_global_shortcut_action(Badge<Action>, Action& action)
|
2019-03-03 11:32:15 +00:00
|
|
|
{
|
2019-04-20 19:56:56 +00:00
|
|
|
m_global_shortcut_actions.set(action.shortcut(), &action);
|
2019-03-03 11:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
|
2019-03-03 11:32:15 +00:00
|
|
|
{
|
2019-04-20 19:56:56 +00:00
|
|
|
m_global_shortcut_actions.remove(action.shortcut());
|
2019-03-03 11:32:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
Action* Application::action_for_key_event(const KeyEvent& event)
|
2019-03-03 11:32:15 +00:00
|
|
|
{
|
2020-02-02 14:07:41 +00:00
|
|
|
auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key()));
|
2019-04-20 19:56:56 +00:00
|
|
|
if (it == m_global_shortcut_actions.end())
|
2019-03-03 11:32:15 +00:00
|
|
|
return nullptr;
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2019-04-08 16:58:44 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
class Application::TooltipWindow final : public Window {
|
2020-02-15 00:27:37 +00:00
|
|
|
C_OBJECT(TooltipWindow);
|
|
|
|
|
2019-04-08 16:58:44 +00:00
|
|
|
public:
|
2020-02-15 00:27:37 +00:00
|
|
|
void set_tooltip(const StringView& tooltip)
|
|
|
|
{
|
2020-03-19 21:29:01 +00:00
|
|
|
// FIXME: Add some kind of GUI::Label auto-sizing feature.
|
2020-02-15 00:27:37 +00:00
|
|
|
int text_width = m_label->font().width(tooltip);
|
2020-08-15 09:44:34 +00:00
|
|
|
set_rect(rect().x(), rect().y(), text_width + 10, m_label->font().glyph_height() + 8);
|
2020-02-15 00:27:37 +00:00
|
|
|
m_label->set_text(tooltip);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-04-08 16:58:44 +00:00
|
|
|
TooltipWindow()
|
|
|
|
{
|
2020-02-02 14:07:41 +00:00
|
|
|
set_window_type(WindowType::Tooltip);
|
2020-03-04 08:46:23 +00:00
|
|
|
m_label = set_main_widget<Label>();
|
2019-04-08 16:58:44 +00:00
|
|
|
m_label->set_background_color(Color::from_rgb(0xdac7b5));
|
|
|
|
m_label->set_fill_with_background_color(true);
|
|
|
|
m_label->set_frame_thickness(1);
|
2020-02-06 10:56:38 +00:00
|
|
|
m_label->set_frame_shape(Gfx::FrameShape::Container);
|
|
|
|
m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
|
2019-04-08 16:58:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
RefPtr<Label> m_label;
|
2019-04-08 16:58:44 +00:00
|
|
|
};
|
|
|
|
|
2020-08-15 09:44:34 +00:00
|
|
|
void Application::show_tooltip(const StringView& tooltip, const Gfx::IntPoint& screen_location, const Widget* tooltip_source_widget)
|
2019-04-08 16:58:44 +00:00
|
|
|
{
|
2020-08-15 09:44:34 +00:00
|
|
|
m_tooltip_source_widget = tooltip_source_widget;
|
2019-04-08 16:58:44 +00:00
|
|
|
if (!m_tooltip_window) {
|
2020-02-15 00:27:37 +00:00
|
|
|
m_tooltip_window = TooltipWindow::construct();
|
2019-04-08 16:58:44 +00:00
|
|
|
m_tooltip_window->set_double_buffering_enabled(false);
|
|
|
|
}
|
|
|
|
m_tooltip_window->set_tooltip(tooltip);
|
2019-12-05 16:59:06 +00:00
|
|
|
|
2020-06-10 08:57:59 +00:00
|
|
|
Gfx::IntRect desktop_rect = Desktop::the().rect();
|
2019-12-05 16:59:06 +00:00
|
|
|
|
|
|
|
const int margin = 30;
|
2020-06-10 08:57:59 +00:00
|
|
|
Gfx::IntPoint adjusted_pos = screen_location;
|
2019-12-05 16:59:06 +00:00
|
|
|
if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
|
|
|
|
adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
|
|
|
|
}
|
|
|
|
if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
|
|
|
|
adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_tooltip_window->move_to(adjusted_pos);
|
2019-04-08 16:58:44 +00:00
|
|
|
m_tooltip_window->show();
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::hide_tooltip()
|
2019-04-08 16:58:44 +00:00
|
|
|
{
|
2020-08-15 09:44:34 +00:00
|
|
|
m_tooltip_source_widget = nullptr;
|
2019-07-23 18:51:08 +00:00
|
|
|
if (m_tooltip_window) {
|
2019-04-08 16:58:44 +00:00
|
|
|
m_tooltip_window->hide();
|
2019-07-23 18:51:08 +00:00
|
|
|
m_tooltip_window = nullptr;
|
|
|
|
}
|
2019-04-08 16:58:44 +00:00
|
|
|
}
|
2019-07-23 16:16:25 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::did_create_window(Badge<Window>)
|
2019-10-25 05:23:35 +00:00
|
|
|
{
|
|
|
|
if (m_event_loop->was_exit_requested())
|
|
|
|
m_event_loop->unquit();
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::did_delete_last_window(Badge<Window>)
|
2019-07-23 16:16:25 +00:00
|
|
|
{
|
|
|
|
if (m_quit_when_last_window_deleted)
|
|
|
|
m_event_loop->quit(0);
|
|
|
|
}
|
2019-12-24 19:57:54 +00:00
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::set_system_palette(SharedBuffer& buffer)
|
2019-12-24 19:57:54 +00:00
|
|
|
{
|
|
|
|
if (!m_system_palette)
|
2020-02-06 10:56:38 +00:00
|
|
|
m_system_palette = Gfx::PaletteImpl::create_with_shared_buffer(buffer);
|
2019-12-24 19:57:54 +00:00
|
|
|
else
|
|
|
|
m_system_palette->replace_internal_buffer({}, buffer);
|
|
|
|
|
|
|
|
if (!m_palette)
|
|
|
|
m_palette = m_system_palette;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
void Application::set_palette(const Palette& palette)
|
2019-12-24 19:57:54 +00:00
|
|
|
{
|
2019-12-28 23:47:49 +00:00
|
|
|
m_palette = palette.impl();
|
2019-12-24 19:57:54 +00:00
|
|
|
}
|
2020-02-02 14:07:41 +00:00
|
|
|
|
2020-02-15 00:18:12 +00:00
|
|
|
Gfx::Palette Application::palette() const
|
|
|
|
{
|
|
|
|
return Palette(*m_palette);
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:07:41 +00:00
|
|
|
}
|