Squashed 'libraries/ZWidget/' changes from 96501b6ef..cecd34301

cecd34301 Delay load DPI-related functions
bfc172f70 Added exception to some unimplemented functions
7c8d469bc Added item modifications functions
847d0e6d9 Improved keyboard navigation
8c4270e0f Dropdown can now be closed by re-selecting it
91402c31e Fix dropdown positioning in x11 and win32
02da8cdc0 Added ListView::GetPreferredWidth, ListView::GetPreferredHeight, ListView::GetMinHeight
04bf153d9 Added event subscriber system
1ce55e5da Added Dropdown Known issues: does not hide when parent widget's visibility changes

git-subtree-dir: libraries/ZWidget
git-subtree-split: cecd34301d369400227eb0f27bad2ab41ad756d2
This commit is contained in:
Mari the Deer 2025-08-13 14:11:06 +02:00
commit 31c8dde32f
12 changed files with 648 additions and 13 deletions

View file

@ -0,0 +1,69 @@
#pragma once
#include <string>
#include <vector>
#include <functional>
#include "../../core/widget.h"
#include "../../widgets/listview/listview.h"
class DropdownList : public ListView
{
public:
DropdownList(Widget* parent, Dropdown* owner);
protected:
void OnKeyDown(InputKey key) override;
Dropdown* owner;
};
class Dropdown : public Widget
{
public:
Dropdown(Widget* parent);
void AddItem(const std::string& text, int index = -1);
bool UpdateItem(const std::string& text, int index);
bool RemoveItem(int index = -1);
void ClearItems();
const std::string& GetItem(int index);
void SetMaxDisplayItems(int items);
void SetDropdownDirection(bool below);
int GetSelectedItem() const { return selectedItem; }
void SetSelectedItem(int index);
double GetPreferredHeight() const;
double GetPreferredWidth() const;
std::function<void(int)> OnChanged;
protected:
void OnPaint(Canvas* canvas) override;
bool OnMouseDown(const Point& pos, InputKey key) override;
void OnKeyDown(InputKey key) override;
void OnGeometryChanged() override;
void OnLostFocus() override;
void Notify(Widget* source, const WidgetEvent type) override;
private:
void ItemsChanged();
bool OpenDropdown();
bool CloseDropdown();
void OnDropdownActivated();
size_t GetDisplayItems();
std::vector<std::string> items;
int selectedItem = -1;
std::string text;
bool dropdownOpen = false;
Widget* dropdown = nullptr;
DropdownList* listView = nullptr;
int maxDisplayItems = 0;
bool dropdownDirection = true;
friend DropdownList;
};