2019-02-11 09:47:10 +01:00
|
|
|
#include "WSMenu.h"
|
|
|
|
#include "WSMenuItem.h"
|
|
|
|
#include "WSWindow.h"
|
|
|
|
#include "WSMessage.h"
|
|
|
|
#include "WSMessageLoop.h"
|
2019-02-11 13:59:26 +01:00
|
|
|
#include "WSWindowManager.h"
|
2019-02-11 09:47:10 +01:00
|
|
|
#include <SharedGraphics/Painter.h>
|
|
|
|
#include <SharedGraphics/Font.h>
|
|
|
|
|
|
|
|
WSMenu::WSMenu(const String& name)
|
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WSMenu::~WSMenu()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenu::set_menu_window(OwnPtr<WSWindow>&& menu_window)
|
|
|
|
{
|
|
|
|
m_menu_window = move(menu_window);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Font& WSMenu::font() const
|
|
|
|
{
|
|
|
|
return Font::default_font();
|
|
|
|
}
|
|
|
|
|
|
|
|
int WSMenu::width() const
|
|
|
|
{
|
|
|
|
int longest = 0;
|
|
|
|
for (auto& item : m_items) {
|
|
|
|
if (item->type() == WSMenuItem::Text)
|
|
|
|
longest = max(longest, font().width(item->text()));
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:19:56 +01:00
|
|
|
return max(longest, rect_in_menubar().width()) + padding() * 2;
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int WSMenu::height() const
|
|
|
|
{
|
|
|
|
if (m_items.is_empty())
|
|
|
|
return 0;
|
|
|
|
return (m_items.last()->rect().bottom() - 1) + padding();
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:08:37 +01:00
|
|
|
void WSMenu::redraw()
|
|
|
|
{
|
|
|
|
ASSERT(menu_window());
|
|
|
|
draw();
|
|
|
|
menu_window()->invalidate();
|
|
|
|
}
|
|
|
|
|
2019-02-11 09:47:10 +01:00
|
|
|
WSWindow& WSMenu::ensure_menu_window()
|
|
|
|
{
|
|
|
|
if (!m_menu_window) {
|
|
|
|
Point next_item_location(padding() / 2, padding() / 2);
|
|
|
|
for (auto& item : m_items) {
|
|
|
|
int height = 0;
|
|
|
|
if (item->type() == WSMenuItem::Text)
|
|
|
|
height = item_height();
|
|
|
|
else if (item->type() == WSMenuItem::Separator)
|
|
|
|
height = 7;
|
|
|
|
item->set_rect({ next_item_location, { width() - padding(), height } });
|
|
|
|
next_item_location.move_by(0, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto window = make<WSWindow>(*this);
|
|
|
|
window->set_rect(0, 0, width(), height());
|
|
|
|
m_menu_window = move(window);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
return *m_menu_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenu::draw()
|
|
|
|
{
|
2019-02-11 10:08:37 +01:00
|
|
|
ASSERT(menu_window());
|
|
|
|
ASSERT(menu_window()->backing());
|
|
|
|
Painter painter(*menu_window()->backing());
|
2019-02-11 09:47:10 +01:00
|
|
|
|
2019-02-11 10:08:37 +01:00
|
|
|
Rect rect { { }, menu_window()->size() };
|
2019-02-11 09:47:10 +01:00
|
|
|
painter.draw_rect(rect, Color::White);
|
|
|
|
painter.fill_rect(rect.shrunken(2, 2), Color::LightGray);
|
|
|
|
|
|
|
|
for (auto& item : m_items) {
|
|
|
|
if (item->type() == WSMenuItem::Text) {
|
|
|
|
Color text_color = Color::Black;
|
|
|
|
if (item.ptr() == m_hovered_item) {
|
2019-02-11 10:08:37 +01:00
|
|
|
painter.fill_rect(item->rect(), Color(0, 0, 104));
|
2019-02-11 09:47:10 +01:00
|
|
|
text_color = Color::White;
|
|
|
|
}
|
2019-02-11 10:08:37 +01:00
|
|
|
painter.draw_text(item->rect(), item->text(), TextAlignment::CenterLeft, text_color);
|
2019-02-11 09:47:10 +01:00
|
|
|
} else if (item->type() == WSMenuItem::Separator) {
|
|
|
|
Point p1(padding(), item->rect().center().y());
|
|
|
|
Point p2(width() - padding(), item->rect().center().y());
|
|
|
|
painter.draw_line(p1, p2, Color::MidGray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSMenu::on_window_message(WSMessage& message)
|
|
|
|
{
|
2019-02-11 10:08:37 +01:00
|
|
|
ASSERT(menu_window());
|
2019-02-11 09:47:10 +01:00
|
|
|
if (message.type() == WSMessage::MouseMove) {
|
2019-02-11 10:08:37 +01:00
|
|
|
auto* item = item_at(static_cast<WSMouseEvent&>(message).position());
|
|
|
|
if (!item || m_hovered_item == item)
|
|
|
|
return;
|
|
|
|
m_hovered_item = item;
|
|
|
|
redraw();
|
2019-02-11 10:55:02 +01:00
|
|
|
return;
|
2019-02-11 10:08:37 +01:00
|
|
|
}
|
2019-02-11 10:55:02 +01:00
|
|
|
|
|
|
|
if (message.type() == WSMessage::MouseUp) {
|
|
|
|
if (!m_hovered_item)
|
|
|
|
return;
|
|
|
|
did_activate(*m_hovered_item);
|
2019-02-11 11:06:41 +01:00
|
|
|
clear_hovered_item();
|
2019-02-11 10:55:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:06:41 +01:00
|
|
|
void WSMenu::clear_hovered_item()
|
|
|
|
{
|
|
|
|
if (!m_hovered_item)
|
|
|
|
return;
|
|
|
|
m_hovered_item = nullptr;
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:55:02 +01:00
|
|
|
void WSMenu::did_activate(WSMenuItem& item)
|
|
|
|
{
|
|
|
|
if (on_item_activation)
|
|
|
|
on_item_activation(item);
|
2019-02-11 13:59:26 +01:00
|
|
|
close();
|
2019-02-11 10:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WSMenuItem* WSMenu::item_at(const Point& position)
|
|
|
|
{
|
|
|
|
for (auto& item : m_items) {
|
|
|
|
if (!item->rect().contains(position))
|
|
|
|
continue;
|
|
|
|
return item.ptr();
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
2019-02-11 10:08:37 +01:00
|
|
|
return nullptr;
|
2019-02-11 09:47:10 +01:00
|
|
|
}
|
2019-02-11 13:59:26 +01:00
|
|
|
|
|
|
|
void WSMenu::close()
|
|
|
|
{
|
|
|
|
WSWindowManager::the().close_menu(*this);
|
|
|
|
};
|