Skip to content

Commit

Permalink
Add tab widget and event propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Jan 12, 2024
1 parent 8113ffb commit fac2262
Show file tree
Hide file tree
Showing 19 changed files with 620 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(ZWIDGET_SOURCES
src/widgets/pushbutton/pushbutton.cpp
src/widgets/checkboxlabel/checkboxlabel.cpp
src/widgets/listview/listview.cpp
src/widgets/tabwidget/tabwidget.cpp
src/window/window.cpp
)

Expand Down Expand Up @@ -58,6 +59,7 @@ set(ZWIDGET_INCLUDES
include/zwidget/widgets/pushbutton/pushbutton.h
include/zwidget/widgets/checkboxlabel/checkboxlabel.h
include/zwidget/widgets/listview/listview.h
include/zwidget/widgets/tabwidget/tabwidget.h
include/zwidget/window/window.h
)

Expand Down Expand Up @@ -91,6 +93,7 @@ source_group("src\\widgets\\textlabel" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURC
source_group("src\\widgets\\pushbutton" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/pushbutton/.+")
source_group("src\\widgets\\checkboxlabel" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/checkboxlabel/.+")
source_group("src\\widgets\\listview" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/listview/.+")
source_group("src\\widgets\\tabwidget" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/tabwidget/.+")
source_group("src\\window" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/window/.+")
source_group("include" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/.+")
source_group("include\\core" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/core/.+")
Expand All @@ -107,6 +110,7 @@ source_group("include\\widgets\\textlabel" REGULAR_EXPRESSION "${CMAKE_CURRENT_S
source_group("include\\widgets\\pushbutton" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/widgets/pushbutton/.+")
source_group("include\\widgets\\checkboxlabel" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/widgets/checkboxlabel/.+")
source_group("include\\widgets\\listview" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/widgets/listview/.+")
source_group("include\\widgets\\tabwidget" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/widgets/tabwidget/.+")
source_group("include\\window" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/window/.+")
source_group("include\\window\\win32" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/window/win32/.+")
source_group("include\\window\\sdl2" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/include/zwidget/window/sdl2/.+")
Expand Down
16 changes: 11 additions & 5 deletions include/zwidget/core/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class Widget : DisplayWindowHost

// Widget noncontent area
void SetNoncontentSizes(double left, double top, double right, double bottom);
double GetNoncontentLeft() const { return Noncontent.Left; }
double GetNoncontentTop() const { return Noncontent.Top; }
double GetNoncontentRight() const { return Noncontent.Right; }
double GetNoncontentBottom() const { return Noncontent.Bottom; }

// Widget frame box
Rect GetFrameGeometry() const;
Expand Down Expand Up @@ -68,6 +72,7 @@ class Widget : DisplayWindowHost
bool HasFocus();
bool IsEnabled();
bool IsVisible();
bool IsHidden();

void SetFocus();
void SetEnabled(bool value);
Expand All @@ -86,7 +91,7 @@ class Widget : DisplayWindowHost
void SetClipboardText(const std::string& text);

Widget* Window();
Canvas* GetCanvas();
Canvas* GetCanvas() const;
Widget* ChildAt(double x, double y) { return ChildAt(Point(x, y)); }
Widget* ChildAt(const Point& pos);

Expand All @@ -109,11 +114,11 @@ class Widget : DisplayWindowHost
protected:
virtual void OnPaintFrame(Canvas* canvas) { }
virtual void OnPaint(Canvas* canvas) { }
virtual bool OnMouseDown(const Point& pos, int key) { return false; }
virtual bool OnMouseDoubleclick(const Point& pos, int key) { return false; }
virtual bool OnMouseUp(const Point& pos, int key) { return false; }
virtual bool OnMouseWheel(const Point& pos, EInputKey key) { return false; }
virtual void OnMouseMove(const Point& pos) { }
virtual void OnMouseDown(const Point& pos, int key) { }
virtual void OnMouseDoubleclick(const Point& pos, int key) { }
virtual void OnMouseUp(const Point& pos, int key) { }
virtual void OnMouseWheel(const Point& pos, EInputKey key) { }
virtual void OnMouseLeave() { }
virtual void OnRawMouseMove(int dx, int dy) { }
virtual void OnKeyChar(std::string chars) { }
Expand Down Expand Up @@ -176,6 +181,7 @@ class Widget : DisplayWindowHost
Widget* FocusWidget = nullptr;
Widget* CaptureWidget = nullptr;
Widget* HoverWidget = nullptr;
bool HiddenFlag = false;

StandardCursor CurrentCursor = StandardCursor::arrow;

Expand Down
4 changes: 2 additions & 2 deletions include/zwidget/widgets/checkboxlabel/checkboxlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CheckboxLabel : public Widget

protected:
void OnPaint(Canvas* canvas) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseUp(const Point& pos, int key) override;
void OnMouseLeave() override;
void OnKeyUp(EInputKey key) override;

Expand Down
6 changes: 3 additions & 3 deletions include/zwidget/widgets/lineedit/lineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class LineEdit : public Widget
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
void OnMouseMove(const Point& pos) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseDoubleclick(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseDoubleclick(const Point& pos, int key) override;
bool OnMouseUp(const Point& pos, int key) override;
void OnKeyChar(std::string chars) override;
void OnKeyDown(EInputKey key) override;
void OnKeyUp(EInputKey key) override;
Expand Down
6 changes: 3 additions & 3 deletions include/zwidget/widgets/listview/listview.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class ListView : public Widget
protected:
void OnPaint(Canvas* canvas) override;
void OnPaintFrame(Canvas* canvas) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseDoubleclick(const Point& pos, int key) override;
void OnMouseWheel(const Point& pos, EInputKey key) override;
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseDoubleclick(const Point& pos, int key) override;
bool OnMouseWheel(const Point& pos, EInputKey key) override;
void OnKeyDown(EInputKey key) override;
void OnGeometryChanged() override;
void OnScrollbarScroll();
Expand Down
4 changes: 2 additions & 2 deletions include/zwidget/widgets/pushbutton/pushbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class PushButton : public Widget
protected:
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseUp(const Point& pos, int key) override;
void OnMouseMove(const Point& pos) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
void OnMouseLeave() override;
void OnKeyDown(EInputKey key) override;
void OnKeyUp(EInputKey key) override;
Expand Down
4 changes: 2 additions & 2 deletions include/zwidget/widgets/scrollbar/scrollbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Scrollbar : public Widget
std::function<void()> FuncScrollEnd;

protected:
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseUp(const Point& pos, int key) override;
void OnMouseMove(const Point& pos) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
void OnMouseLeave() override;
void OnPaint(Canvas* canvas) override;
void OnEnableChanged() override;
Expand Down
124 changes: 124 additions & 0 deletions include/zwidget/widgets/tabwidget/tabwidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

#pragma once

#include "../../core/widget.h"
#include <functional>
#include <string>
#include <vector>

class TabBar;
class TabBarTab;
class TabWidgetStack;
class TextLabel;
class ImageBox;
class Image;

class TabWidget : public Widget
{
public:
TabWidget(Widget* parent);

int AddTab(Widget* page, const std::string& label);
int AddTab(Widget* page, const std::shared_ptr<Image>& icon, const std::string& label);

void SetTabText(int index, const std::string& text);
void SetTabText(Widget* page, const std::string& text);
void SetTabIcon(int index, const std::shared_ptr<Image>& icon);
void SetTabIcon(Widget* page, const std::shared_ptr<Image>& icon);

int GetCurrentIndex() const;
Widget* GetCurrentWidget() const;

int GetPageIndex(Widget* pageWidget) const;

void SetCurrentIndex(int pageIndex);
void SetCurrentWidget(Widget* pageWidget);

std::function<void()> OnCurrentChanged;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;

private:
void OnBarCurrentChanged();

TabBar* Bar = nullptr;
TabWidgetStack* PageStack = nullptr;
std::vector<Widget*> Pages;
};

class TabBar : public Widget
{
public:
TabBar(Widget* parent);

int AddTab(const std::string& label);
int AddTab(const std::shared_ptr<Image>& icon, const std::string& label);

void SetTabText(int index, const std::string& text);
void SetTabIcon(int index, const std::shared_ptr<Image>& icon);

int GetCurrentIndex() const;
void SetCurrentIndex(int pageIndex);

double GetPreferredHeight() const { return 30.0; }

std::function<void()> OnCurrentChanged;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;

private:
void OnTabClicked(TabBarTab* tab);
int GetTabIndex(TabBarTab* tab);

int CurrentIndex = -1;
std::vector<TabBarTab*> Tabs;
};

class TabBarTab : public Widget
{
public:
TabBarTab(Widget* parent);

void SetText(const std::string& text);
void SetIcon(const std::shared_ptr<Image>& icon);
void SetCurrent(bool value);

double GetPreferredWidth() const;

std::function<void()> OnClick;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseUp(const Point& pos, int key) override;
void OnMouseMove(const Point& pos) override;
void OnMouseLeave() override;

private:
bool IsCurrent = false;

ImageBox* Icon = nullptr;
TextLabel* Label = nullptr;
bool hot = false;
};

class TabWidgetStack : public Widget
{
public:
TabWidgetStack(Widget* parent);

void SetCurrentWidget(Widget* widget);
Widget* GetCurrentWidget() const { return CurrentWidget; }

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;

private:
Widget* CurrentWidget = nullptr;
};
6 changes: 3 additions & 3 deletions include/zwidget/widgets/textedit/textedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class TextEdit : public Widget
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
void OnMouseMove(const Point& pos) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseDoubleclick(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
bool OnMouseDown(const Point& pos, int key) override;
bool OnMouseDoubleclick(const Point& pos, int key) override;
bool OnMouseUp(const Point& pos, int key) override;
void OnKeyChar(std::string chars) override;
void OnKeyDown(EInputKey key) override;
void OnKeyUp(EInputKey key) override;
Expand Down
1 change: 1 addition & 0 deletions include/zwidget/widgets/textlabel/textlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TextLabel : public Widget
void SetTextAlignment(TextLabelAlignment alignment);
TextLabelAlignment GetTextAlignment() const;

double GetPreferredWidth() const;
double GetPreferredHeight() const;

protected:
Expand Down
Loading

0 comments on commit fac2262

Please sign in to comment.