2019-02-11 14:43:43 +01:00
|
|
|
#include <LibGUI/GApplication.h>
|
|
|
|
|
#include <LibGUI/GEventLoop.h>
|
|
|
|
|
#include <LibGUI/GMenuBar.h>
|
2019-03-03 12:32:15 +01:00
|
|
|
#include <LibGUI/GAction.h>
|
2019-02-11 14:43:43 +01:00
|
|
|
|
2019-02-11 14:56:23 +01:00
|
|
|
static GApplication* s_the;
|
|
|
|
|
|
|
|
|
|
GApplication& GApplication::the()
|
|
|
|
|
{
|
|
|
|
|
ASSERT(s_the);
|
|
|
|
|
return *s_the;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 14:43:43 +01:00
|
|
|
GApplication::GApplication(int argc, char** argv)
|
|
|
|
|
{
|
2019-02-25 19:05:51 +01:00
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
2019-02-11 14:56:23 +01:00
|
|
|
ASSERT(!s_the);
|
|
|
|
|
s_the = this;
|
2019-02-11 14:43:43 +01:00
|
|
|
m_event_loop = make<GEventLoop>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GApplication::~GApplication()
|
|
|
|
|
{
|
2019-03-05 12:48:59 +01:00
|
|
|
s_the = nullptr;
|
2019-02-11 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GApplication::exec()
|
|
|
|
|
{
|
2019-03-05 12:48:59 +01:00
|
|
|
int exit_code = m_event_loop->exec();
|
|
|
|
|
// NOTE: Maybe it would be cool to return instead of exit()?
|
|
|
|
|
// This would require cleaning up all the GObjects on the heap.
|
|
|
|
|
exit(exit_code);
|
|
|
|
|
return exit_code;
|
2019-02-11 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-17 09:58:35 +01:00
|
|
|
void GApplication::quit(int exit_code)
|
2019-02-11 14:56:23 +01:00
|
|
|
{
|
2019-02-17 09:58:35 +01:00
|
|
|
m_event_loop->quit(exit_code);
|
2019-02-11 14:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-11 14:43:43 +01:00
|
|
|
void GApplication::set_menubar(OwnPtr<GMenuBar>&& menubar)
|
|
|
|
|
{
|
2019-02-11 15:37:12 +01:00
|
|
|
if (m_menubar)
|
|
|
|
|
m_menubar->notify_removed_from_application(Badge<GApplication>());
|
2019-02-11 14:43:43 +01:00
|
|
|
m_menubar = move(menubar);
|
2019-02-11 15:37:12 +01:00
|
|
|
if (m_menubar)
|
|
|
|
|
m_menubar->notify_added_to_application(Badge<GApplication>());
|
2019-02-11 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-03 12:32:15 +01:00
|
|
|
void GApplication::register_shortcut_action(Badge<GAction>, GAction& action)
|
|
|
|
|
{
|
|
|
|
|
m_shortcut_actions.set(action.shortcut(), &action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GApplication::unregister_shortcut_action(Badge<GAction>, GAction& action)
|
|
|
|
|
{
|
|
|
|
|
m_shortcut_actions.remove(action.shortcut());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GAction* GApplication::action_for_key_event(const GKeyEvent& event)
|
|
|
|
|
{
|
|
|
|
|
auto it = m_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
|
|
|
|
|
if (it == m_shortcut_actions.end())
|
|
|
|
|
return nullptr;
|
|
|
|
|
return (*it).value;
|
|
|
|
|
}
|