Update to latest ZWidget

This commit is contained in:
Magnus Norddahl 2024-05-26 00:14:44 +02:00
commit d24d5ae2c8
16 changed files with 578 additions and 36 deletions

View file

@ -41,16 +41,24 @@ double CheckboxLabel::GetPreferredHeight() const
void CheckboxLabel::OnPaint(Canvas* canvas)
{
// To do: add and use GetStyleImage for the checkbox
double center = GridFitPoint(GetHeight() * 0.5);
double borderwidth = GridFitSize(1.0);
double outerboxsize = GridFitSize(10.0);
double innerboxsize = outerboxsize - 2.0 * borderwidth;
double checkedsize = innerboxsize - 2.0 * borderwidth;
if (checked)
{
canvas->fillRect(Rect::xywh(0.0, GetHeight() * 0.5 - 6.0, 10.0, 10.0), GetStyleColor("checked-outer-border-color"));
canvas->fillRect(Rect::xywh(1.0, GetHeight() * 0.5 - 5.0, 8.0, 8.0), GetStyleColor("checked-inner-border-color"));
canvas->fillRect(Rect::xywh(2.0, GetHeight() * 0.5 - 4.0, 6.0, 6.0), GetStyleColor("checked-color"));
canvas->fillRect(Rect::xywh(0.0, center - 6.0 * borderwidth, outerboxsize, outerboxsize), GetStyleColor("checked-outer-border-color"));
canvas->fillRect(Rect::xywh(1.0 * borderwidth, center - 5.0 * borderwidth, innerboxsize, innerboxsize), GetStyleColor("checked-inner-border-color"));
canvas->fillRect(Rect::xywh(2.0 * borderwidth, center - 4.0 * borderwidth, checkedsize, checkedsize), GetStyleColor("checked-color"));
}
else
{
canvas->fillRect(Rect::xywh(0.0, GetHeight() * 0.5 - 6.0, 10.0, 10.0), GetStyleColor("unchecked-outer-border-color"));
canvas->fillRect(Rect::xywh(1.0, GetHeight() * 0.5 - 5.0, 8.0, 8.0), GetStyleColor("unchecked-inner-border-color"));
canvas->fillRect(Rect::xywh(0.0, center - 6.0 * borderwidth, outerboxsize, outerboxsize), GetStyleColor("unchecked-outer-border-color"));
canvas->fillRect(Rect::xywh(1.0 * borderwidth, center - 5.0 * borderwidth, innerboxsize, innerboxsize), GetStyleColor("unchecked-inner-border-color"));
}
canvas->drawText(Point(14.0, GetHeight() - 5.0), GetStyleColor("color"), text);

View file

@ -5,6 +5,14 @@ ImageBox::ImageBox(Widget* parent) : Widget(parent)
{
}
double ImageBox::GetPreferredWidth() const
{
if (image)
return (double)image->GetWidth();
else
return 0.0;
}
double ImageBox::GetPreferredHeight() const
{
if (image)

View file

@ -1,4 +1,4 @@
#include <algorithm>
#include "widgets/lineedit/lineedit.h"
#include "core/utf8reader.h"
#include "core/colorf.h"

View file

@ -4,13 +4,214 @@
Menubar::Menubar(Widget* parent) : Widget(parent)
{
SetStyleClass("menubar");
}
Menubar::~Menubar()
{
}
void Menubar::OnPaint(Canvas* canvas)
MenubarItem* Menubar::AddItem(std::string text, std::function<void(Menu* menu)> onOpen, bool alignRight)
{
canvas->drawText(Point(16.0, 21.0), Colorf::fromRgba8(0, 0, 0), "File Edit View Tools Window Help");
auto item = new MenubarItem(this, text, alignRight);
item->SetOpenCallback(std::move(onOpen));
menuItems.push_back(item);
OnGeometryChanged();
return item;
}
void Menubar::ShowMenu(MenubarItem* item)
{
CloseMenu();
if (item->GetOpenCallback())
{
openMenu = new Menu(this);
openMenu->onCloseMenu = [=]() { CloseMenu(); };
item->GetOpenCallback()(openMenu);
if (item->AlignRight)
openMenu->SetRightPosition(item->MapToGlobal(Point(item->GetWidth(), item->GetHeight())));
else
openMenu->SetLeftPosition(item->MapToGlobal(Point(0.0, item->GetHeight())));
openMenu->Show();
}
}
void Menubar::CloseMenu()
{
//delete openMenu;
//openMenu = nullptr;
}
void Menubar::OnGeometryChanged()
{
double w = GetWidth();
double h = GetHeight();
double left = 0.0;
double right = w;
for (MenubarItem* item : menuItems)
{
double itemwidth = item->GetPreferredWidth();
itemwidth += 16.0;
if (!item->AlignRight)
{
item->SetFrameGeometry(left, 0.0, itemwidth, h);
left += itemwidth;
}
else
{
right -= itemwidth;
item->SetFrameGeometry(right, 0.0, itemwidth, h);
}
}
}
/////////////////////////////////////////////////////////////////////////////
MenubarItem::MenubarItem(Menubar* menubar, std::string text, bool alignRight) : Widget(menubar), menubar(menubar), text(text), AlignRight(alignRight)
{
SetStyleClass("menubaritem");
}
bool MenubarItem::OnMouseDown(const Point& pos, InputKey key)
{
menubar->ShowMenu(this);
return true;
}
bool MenubarItem::OnMouseUp(const Point& pos, InputKey key)
{
return true;
}
void MenubarItem::OnMouseMove(const Point& pos)
{
if (GetStyleState().empty())
{
SetStyleState("hover");
}
}
void MenubarItem::OnMouseLeave()
{
SetStyleState("");
}
double MenubarItem::GetPreferredWidth() const
{
Canvas* canvas = GetCanvas();
return canvas->measureText(text).width;
}
void MenubarItem::OnPaint(Canvas* canvas)
{
double x = (GetWidth() - canvas->measureText(text).width) * 0.5;
canvas->drawText(Point(x, 21.0), GetStyleColor("color"), text);
}
/////////////////////////////////////////////////////////////////////////////
Menu::Menu(Widget* parent) : Widget(parent, WidgetType::Popup)
{
SetStyleClass("menu");
}
void Menu::SetLeftPosition(const Point& pos)
{
SetFrameGeometry(Rect::xywh(pos.x, pos.y, GetPreferredWidth() + GetNoncontentLeft() + GetNoncontentRight(), GetPreferredHeight() + GetNoncontentTop() + GetNoncontentBottom()));
}
void Menu::SetRightPosition(const Point& pos)
{
SetFrameGeometry(Rect::xywh(pos.x - GetWidth() - GetNoncontentLeft() - GetNoncontentRight(), pos.y, GetWidth() + GetNoncontentLeft() + GetNoncontentRight(), GetHeight() + GetNoncontentTop() + GetNoncontentBottom()));
}
MenuItem* Menu::AddItem(std::shared_ptr<Image> icon, std::string text, std::function<void()> onClick)
{
auto item = new MenuItem(this);
if (icon)
item->icon->SetImage(icon);
item->text->SetText(text);
/*
item->element->addEventListener("click", [=](Event* event)
{
event->stopPropagation();
if (onCloseMenu)
onCloseMenu();
if (onClick)
onClick();
});
*/
return item;
}
MenuItemSeparator* Menu::AddSeparator()
{
auto sep = new MenuItemSeparator(this);
return sep;
}
double Menu::GetPreferredWidth() const
{
return 200.0;
}
double Menu::GetPreferredHeight() const
{
double h = 0.0;
for (Widget* item = FirstChild(); item != nullptr; item = item->NextSibling())
{
h += 20.0;
}
return h;
}
void Menu::OnGeometryChanged()
{
double w = GetWidth();
double h = GetHeight();
double y = 0.0;
for (Widget* item = FirstChild(); item != nullptr; item = item->NextSibling())
{
item->SetFrameGeometry(Rect::xywh(0.0, y, w, 20.0));
y += 20.0;
}
}
/////////////////////////////////////////////////////////////////////////////
MenuItem::MenuItem(Widget* parent) : Widget(parent)
{
SetStyleClass("menuitem");
icon = new ImageBox(this);
text = new TextLabel(this);
}
void MenuItem::OnMouseMove(const Point& pos)
{
if (GetStyleState().empty())
{
SetStyleState("hover");
}
}
void MenuItem::OnMouseLeave()
{
SetStyleState("");
}
void MenuItem::OnGeometryChanged()
{
double iconwidth = icon->GetPreferredWidth();
double iconheight = icon->GetPreferredHeight();
double w = GetWidth();
double h = GetHeight();
icon->SetFrameGeometry(Rect::xywh(0.0, (h - iconheight) * 0.5, iconwidth, iconheight));
text->SetFrameGeometry(Rect::xywh(iconwidth, 0.0, w - iconwidth, h));
}
/////////////////////////////////////////////////////////////////////////////
MenuItemSeparator::MenuItemSeparator(Widget* parent) : Widget(parent)
{
SetStyleClass("menuitemseparator");
}