2019-02-11 16:43:43 +03:00
|
|
|
#include <LibGUI/GApplication.h>
|
|
|
|
#include <LibGUI/GEventLoop.h>
|
|
|
|
#include <LibGUI/GMenuBar.h>
|
2019-03-03 14:32:15 +03:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-04-08 19:58:44 +03:00
|
|
|
#include <LibGUI/GWindow.h>
|
|
|
|
#include <LibGUI/GLabel.h>
|
|
|
|
#include <LibGUI/GPainter.h>
|
|
|
|
#include <WindowServer/WSAPITypes.h>
|
2019-02-11 16:43:43 +03:00
|
|
|
|
2019-02-11 16:56:23 +03:00
|
|
|
static GApplication* s_the;
|
|
|
|
|
|
|
|
GApplication& GApplication::the()
|
|
|
|
{
|
|
|
|
ASSERT(s_the);
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:43:43 +03:00
|
|
|
GApplication::GApplication(int argc, char** argv)
|
|
|
|
{
|
2019-02-25 21:05:51 +03:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2019-02-11 16:56:23 +03:00
|
|
|
ASSERT(!s_the);
|
|
|
|
s_the = this;
|
2019-02-11 16:43:43 +03:00
|
|
|
m_event_loop = make<GEventLoop>();
|
|
|
|
}
|
|
|
|
|
|
|
|
GApplication::~GApplication()
|
|
|
|
{
|
2019-03-05 14:48:59 +03:00
|
|
|
s_the = nullptr;
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int GApplication::exec()
|
|
|
|
{
|
2019-03-05 14:48:59 +03:00
|
|
|
int exit_code = m_event_loop->exec();
|
|
|
|
// NOTE: Maybe it would be cool to return instead of exit()?
|
2019-04-10 18:01:54 +03:00
|
|
|
// This would require cleaning up all the CObjects on the heap.
|
2019-03-05 14:48:59 +03:00
|
|
|
exit(exit_code);
|
|
|
|
return exit_code;
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 11:58:35 +03:00
|
|
|
void GApplication::quit(int exit_code)
|
2019-02-11 16:56:23 +03:00
|
|
|
{
|
2019-02-17 11:58:35 +03:00
|
|
|
m_event_loop->quit(exit_code);
|
2019-02-11 16:56:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-11 16:43:43 +03:00
|
|
|
void GApplication::set_menubar(OwnPtr<GMenuBar>&& menubar)
|
|
|
|
{
|
2019-02-11 17:37:12 +03:00
|
|
|
if (m_menubar)
|
|
|
|
m_menubar->notify_removed_from_application(Badge<GApplication>());
|
2019-02-11 16:43:43 +03:00
|
|
|
m_menubar = move(menubar);
|
2019-02-11 17:37:12 +03:00
|
|
|
if (m_menubar)
|
|
|
|
m_menubar->notify_added_to_application(Badge<GApplication>());
|
2019-02-11 16:43:43 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 22:56:56 +03:00
|
|
|
void GApplication::register_global_shortcut_action(Badge<GAction>, GAction& action)
|
2019-03-03 14:32:15 +03:00
|
|
|
{
|
2019-04-20 22:56:56 +03:00
|
|
|
m_global_shortcut_actions.set(action.shortcut(), &action);
|
2019-03-03 14:32:15 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 22:56:56 +03:00
|
|
|
void GApplication::unregister_global_shortcut_action(Badge<GAction>, GAction& action)
|
2019-03-03 14:32:15 +03:00
|
|
|
{
|
2019-04-20 22:56:56 +03:00
|
|
|
m_global_shortcut_actions.remove(action.shortcut());
|
2019-03-03 14:32:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
GAction* GApplication::action_for_key_event(const GKeyEvent& event)
|
|
|
|
{
|
2019-04-20 22:56:56 +03:00
|
|
|
auto it = m_global_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
|
|
|
|
if (it == m_global_shortcut_actions.end())
|
2019-03-03 14:32:15 +03:00
|
|
|
return nullptr;
|
|
|
|
return (*it).value;
|
|
|
|
}
|
2019-04-08 19:58:44 +03:00
|
|
|
|
|
|
|
class GApplication::TooltipWindow final : public GWindow {
|
|
|
|
public:
|
|
|
|
TooltipWindow()
|
|
|
|
{
|
|
|
|
set_window_type(GWindowType::Tooltip);
|
|
|
|
m_label = new GLabel;
|
|
|
|
m_label->set_background_color(Color::from_rgb(0xdac7b5));
|
|
|
|
m_label->set_fill_with_background_color(true);
|
|
|
|
m_label->set_frame_thickness(1);
|
2019-04-10 04:43:46 +03:00
|
|
|
m_label->set_frame_shape(FrameShape::Container);
|
|
|
|
m_label->set_frame_shadow(FrameShadow::Plain);
|
2019-04-08 19:58:44 +03:00
|
|
|
set_main_widget(m_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_tooltip(const String& tooltip)
|
|
|
|
{
|
|
|
|
// FIXME: Add some kind of GLabel auto-sizing feature.
|
|
|
|
int text_width = m_label->font().width(tooltip);
|
|
|
|
set_rect(100, 100, text_width + 10, m_label->font().glyph_height() + 8);
|
|
|
|
m_label->set_text(tooltip);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLabel* m_label { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
void GApplication::show_tooltip(const String& tooltip, const Point& screen_location)
|
|
|
|
{
|
|
|
|
if (!m_tooltip_window) {
|
|
|
|
m_tooltip_window = new TooltipWindow;
|
|
|
|
m_tooltip_window->set_double_buffering_enabled(false);
|
|
|
|
}
|
|
|
|
m_tooltip_window->set_tooltip(tooltip);
|
|
|
|
m_tooltip_window->move_to(screen_location);
|
|
|
|
m_tooltip_window->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GApplication::hide_tooltip()
|
|
|
|
{
|
|
|
|
if (m_tooltip_window)
|
|
|
|
m_tooltip_window->hide();
|
|
|
|
}
|