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:
parent
d9b2c00228
commit
31c8dde32f
12 changed files with 648 additions and 13 deletions
|
|
@ -4,6 +4,7 @@
|
|||
#include <memory>
|
||||
#include <variant>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include "canvas.h"
|
||||
#include "rect.h"
|
||||
#include "colorf.h"
|
||||
|
|
@ -11,6 +12,7 @@
|
|||
|
||||
class Canvas;
|
||||
class Timer;
|
||||
class Dropdown;
|
||||
|
||||
enum class WidgetType
|
||||
{
|
||||
|
|
@ -19,6 +21,13 @@ enum class WidgetType
|
|||
Popup
|
||||
};
|
||||
|
||||
enum class WidgetEvent
|
||||
{
|
||||
VisibilityChange,
|
||||
|
||||
// TODO: add more events
|
||||
};
|
||||
|
||||
class Widget : DisplayWindowHost
|
||||
{
|
||||
public:
|
||||
|
|
@ -93,6 +102,9 @@ public:
|
|||
void ShowNormal();
|
||||
void Hide();
|
||||
|
||||
void Subscribe(Widget* subscriber);
|
||||
void Unsubscribe(Widget* subscriber);
|
||||
|
||||
void ActivateWindow();
|
||||
|
||||
void Close();
|
||||
|
|
@ -178,6 +190,8 @@ protected:
|
|||
virtual void OnLostFocus() { }
|
||||
virtual void OnEnableChanged() { }
|
||||
|
||||
virtual void Notify(Widget* source, const WidgetEvent type) { };
|
||||
|
||||
private:
|
||||
void DetachFromParent();
|
||||
|
||||
|
|
@ -201,6 +215,8 @@ private:
|
|||
void OnWindowDeactivated() override;
|
||||
void OnWindowDpiScaleChanged() override;
|
||||
|
||||
void NotifySubscribers(const WidgetEvent type);
|
||||
|
||||
WidgetType Type = {};
|
||||
|
||||
Widget* ParentObj = nullptr;
|
||||
|
|
@ -234,8 +250,12 @@ private:
|
|||
Widget(const Widget&) = delete;
|
||||
Widget& operator=(const Widget&) = delete;
|
||||
|
||||
std::unordered_set<Widget*> Subscribers;
|
||||
std::unordered_set<Widget*> Subscriptions;
|
||||
|
||||
friend class Timer;
|
||||
friend class OpenFileDialog;
|
||||
friend class OpenFolderDialog;
|
||||
friend class SaveFileDialog;
|
||||
friend class Dropdown;
|
||||
};
|
||||
|
|
|
|||
69
include/zwidget/widgets/dropdown/dropdown.h
Normal file
69
include/zwidget/widgets/dropdown/dropdown.h
Normal 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;
|
||||
};
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
#include <functional>
|
||||
|
||||
class Scrollbar;
|
||||
class Dropdown;
|
||||
|
||||
class ListView : public Widget
|
||||
{
|
||||
|
|
@ -22,6 +23,10 @@ public:
|
|||
void SetSelectedItem(int index);
|
||||
void ScrollToItem(int index);
|
||||
|
||||
double GetPreferredWidth();
|
||||
double GetPreferredHeight();
|
||||
double GetMinimumHeight() const;
|
||||
|
||||
void Activate();
|
||||
|
||||
std::function<void(int)> OnChanged;
|
||||
|
|
@ -43,4 +48,6 @@ protected:
|
|||
std::vector<std::vector<std::string>> items;
|
||||
std::vector<double> columnwidths;
|
||||
int selectedItem = 0;
|
||||
|
||||
friend Dropdown;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue