Add zwidget
This commit is contained in:
parent
b30f1abe25
commit
b2d2f61be0
54 changed files with 9321 additions and 0 deletions
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class CheckboxLabel : public Widget
|
||||
{
|
||||
public:
|
||||
CheckboxLabel(Widget* parent = nullptr);
|
||||
|
||||
void SetText(const std::string& value);
|
||||
const std::string& GetText() const;
|
||||
|
||||
void SetChecked(bool value);
|
||||
bool GetChecked() const;
|
||||
|
||||
double GetPreferredHeight() const;
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
|
||||
private:
|
||||
std::string text;
|
||||
bool checked = false;
|
||||
};
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
#include "../../core/image.h"
|
||||
|
||||
class ImageBox : public Widget
|
||||
{
|
||||
public:
|
||||
ImageBox(Widget* parent);
|
||||
|
||||
void SetImage(std::shared_ptr<Image> newImage);
|
||||
|
||||
double GetPreferredHeight() const;
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Image> image;
|
||||
};
|
||||
160
libraries/ZWidget/include/zwidget/widgets/lineedit/lineedit.h
Normal file
160
libraries/ZWidget/include/zwidget/widgets/lineedit/lineedit.h
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
#include "../../core/timer.h"
|
||||
#include <functional>
|
||||
|
||||
class LineEdit : public Widget
|
||||
{
|
||||
public:
|
||||
LineEdit(Widget* parent);
|
||||
~LineEdit();
|
||||
|
||||
enum Alignment
|
||||
{
|
||||
align_left,
|
||||
align_center,
|
||||
align_right
|
||||
};
|
||||
|
||||
Alignment GetAlignment() const;
|
||||
bool IsReadOnly() const;
|
||||
bool IsLowercase() const;
|
||||
bool IsUppercase() const;
|
||||
bool IsPasswordMode() const;
|
||||
int GetMaxLength() const;
|
||||
|
||||
std::string GetText() const;
|
||||
int GetTextInt() const;
|
||||
float GetTextFloat() const;
|
||||
|
||||
std::string GetSelection() const;
|
||||
int GetSelectionStart() const;
|
||||
int GetSelectionLength() const;
|
||||
|
||||
int GetCursorPos() const;
|
||||
Size GetTextSize();
|
||||
|
||||
Size GetTextSize(const std::string& str);
|
||||
double GetPreferredContentWidth();
|
||||
double GetPreferredContentHeight(double width);
|
||||
|
||||
void SetSelectAllOnFocusGain(bool enable);
|
||||
void SelectAll();
|
||||
void SetAlignment(Alignment alignment);
|
||||
void SetReadOnly(bool enable = true);
|
||||
void SetLowercase(bool enable = true);
|
||||
void SetUppercase(bool enable = true);
|
||||
void SetPasswordMode(bool enable = true);
|
||||
void SetNumericMode(bool enable = true, bool decimals = false);
|
||||
void SetMaxLength(int length);
|
||||
void SetText(const std::string& text);
|
||||
void SetTextInt(int number);
|
||||
void SetTextFloat(float number, int num_decimal_places = 6);
|
||||
void SetSelection(int pos, int length);
|
||||
void ClearSelection();
|
||||
void SetCursorPos(int pos);
|
||||
void DeleteSelectedText();
|
||||
void SetInputMask(const std::string& mask);
|
||||
void SetDecimalCharacter(const std::string& decimal_char);
|
||||
|
||||
std::function<bool(int key)> FuncIgnoreKeyDown;
|
||||
std::function<std::string(std::string text)> FuncFilterKeyChar;
|
||||
std::function<void()> FuncBeforeEditChanged;
|
||||
std::function<void()> FuncAfterEditChanged;
|
||||
std::function<void()> FuncSelectionChanged;
|
||||
std::function<void()> FuncFocusGained;
|
||||
std::function<void()> FuncFocusLost;
|
||||
std::function<void()> FuncEnterPressed;
|
||||
|
||||
protected:
|
||||
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;
|
||||
void OnKeyChar(std::string chars) override;
|
||||
void OnKeyDown(EInputKey key) override;
|
||||
void OnKeyUp(EInputKey key) override;
|
||||
void OnGeometryChanged() override;
|
||||
void OnEnableChanged() override;
|
||||
void OnSetFocus() override;
|
||||
void OnLostFocus() override;
|
||||
|
||||
private:
|
||||
void OnTimerExpired();
|
||||
void OnScrollTimerExpired();
|
||||
void UpdateTextClipping();
|
||||
|
||||
void Move(int steps, bool ctrl, bool shift);
|
||||
bool InsertText(int pos, const std::string& str);
|
||||
void Backspace();
|
||||
void Del();
|
||||
int GetCharacterIndex(double x);
|
||||
int FindNextBreakCharacter(int pos);
|
||||
int FindPreviousBreakCharacter(int pos);
|
||||
std::string GetVisibleTextBeforeSelection();
|
||||
std::string GetVisibleTextAfterSelection();
|
||||
std::string GetVisibleSelectedText();
|
||||
std::string CreatePassword(std::string::size_type num_letters) const;
|
||||
Size GetVisualTextSize(Canvas* canvas, int pos, int npos) const;
|
||||
Size GetVisualTextSize(Canvas* canvas) const;
|
||||
Rect GetCursorRect();
|
||||
Rect GetSelectionRect();
|
||||
bool InputMaskAcceptsInput(int cursor_pos, const std::string& str);
|
||||
void SetSelectionStart(int start);
|
||||
void SetSelectionLength(int length);
|
||||
void SetTextSelection(int start, int length);
|
||||
|
||||
static std::string ToFixed(float number, int num_decimal_places);
|
||||
static std::string ToLower(const std::string& text);
|
||||
static std::string ToUpper(const std::string& text);
|
||||
|
||||
Timer* timer = nullptr;
|
||||
std::string text;
|
||||
Alignment alignment = align_left;
|
||||
int cursor_pos = 0;
|
||||
int max_length = -1;
|
||||
bool mouse_selecting = false;
|
||||
bool lowercase = false;
|
||||
bool uppercase = false;
|
||||
bool password_mode = false;
|
||||
bool numeric_mode = false;
|
||||
bool numeric_mode_decimals = false;
|
||||
bool readonly = false;
|
||||
int selection_start = -1;
|
||||
int selection_length = 0;
|
||||
std::string input_mask;
|
||||
std::string decimal_char = ".";
|
||||
|
||||
VerticalTextPosition vertical_text_align;
|
||||
Timer* scroll_timer = nullptr;
|
||||
|
||||
bool mouse_moves_left = false;
|
||||
bool cursor_blink_visible = true;
|
||||
unsigned int blink_timer = 0;
|
||||
int clip_start_offset = 0;
|
||||
int clip_end_offset = 0;
|
||||
bool ignore_mouse_events = false;
|
||||
|
||||
struct UndoInfo
|
||||
{
|
||||
/* set undo text when:
|
||||
- added char after moving
|
||||
- destructive block operation (del, cut etc)
|
||||
- beginning erase
|
||||
*/
|
||||
std::string undo_text;
|
||||
bool first_erase = false;
|
||||
bool first_text_insert = false;
|
||||
};
|
||||
|
||||
UndoInfo undo_info;
|
||||
|
||||
bool select_all_on_focus_gain = true;
|
||||
|
||||
static const std::string break_characters;
|
||||
static const std::string numeric_mode_characters;
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
#include <vector>
|
||||
|
||||
class ListView : public Widget
|
||||
{
|
||||
public:
|
||||
ListView(Widget* parent = nullptr);
|
||||
|
||||
void AddItem(const std::string& text);
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
|
||||
std::vector<std::string> items;
|
||||
};
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class Menubar;
|
||||
class Toolbar;
|
||||
class Statusbar;
|
||||
|
||||
class MainWindow : public Widget
|
||||
{
|
||||
public:
|
||||
MainWindow();
|
||||
~MainWindow();
|
||||
|
||||
Menubar* GetMenubar() const { return MenubarWidget; }
|
||||
Toolbar* GetToolbar() const { return ToolbarWidget; }
|
||||
Statusbar* GetStatusbar() const { return StatusbarWidget; }
|
||||
Widget* GetCentralWidget() const { return CentralWidget; }
|
||||
|
||||
void SetCentralWidget(Widget* widget);
|
||||
|
||||
protected:
|
||||
void OnGeometryChanged() override;
|
||||
|
||||
private:
|
||||
Menubar* MenubarWidget = nullptr;
|
||||
Toolbar* ToolbarWidget = nullptr;
|
||||
Widget* CentralWidget = nullptr;
|
||||
Statusbar* StatusbarWidget = nullptr;
|
||||
};
|
||||
14
libraries/ZWidget/include/zwidget/widgets/menubar/menubar.h
Normal file
14
libraries/ZWidget/include/zwidget/widgets/menubar/menubar.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class Menubar : public Widget
|
||||
{
|
||||
public:
|
||||
Menubar(Widget* parent);
|
||||
~Menubar();
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
};
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class PushButton : public Widget
|
||||
{
|
||||
public:
|
||||
PushButton(Widget* parent = nullptr);
|
||||
|
||||
void SetText(const std::string& value);
|
||||
const std::string& GetText() const;
|
||||
|
||||
double GetPreferredHeight() const;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
|
||||
private:
|
||||
std::string text;
|
||||
};
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
#include "../../core/timer.h"
|
||||
#include <functional>
|
||||
|
||||
class Scrollbar : public Widget
|
||||
{
|
||||
public:
|
||||
Scrollbar(Widget* parent);
|
||||
~Scrollbar();
|
||||
|
||||
bool IsVertical() const;
|
||||
bool IsHorizontal() const;
|
||||
int GetMin() const;
|
||||
int GetMax() const;
|
||||
int GetLineStep() const;
|
||||
int GetPageStep() const;
|
||||
int GetPosition() const;
|
||||
|
||||
void SetVertical();
|
||||
void SetHorizontal();
|
||||
|
||||
void SetMin(int scroll_min);
|
||||
void SetMax(int scroll_max);
|
||||
void SetLineStep(int step);
|
||||
void SetPageStep(int step);
|
||||
|
||||
void SetRanges(int scroll_min, int scroll_max, int line_step, int page_step);
|
||||
void SetRanges(int view_size, int total_size);
|
||||
|
||||
void SetPosition(int pos);
|
||||
|
||||
std::function<void()> FuncScroll;
|
||||
std::function<void()> FuncScrollMin;
|
||||
std::function<void()> FuncScrollMax;
|
||||
std::function<void()> FuncScrollLineDecrement;
|
||||
std::function<void()> FuncScrollLineIncrement;
|
||||
std::function<void()> FuncScrollPageDecrement;
|
||||
std::function<void()> FuncScrollPageIncrement;
|
||||
std::function<void()> FuncScrollThumbRelease;
|
||||
std::function<void()> FuncScrollThumbTrack;
|
||||
std::function<void()> FuncScrollEnd;
|
||||
|
||||
protected:
|
||||
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;
|
||||
void OnGeometryChanged() override;
|
||||
|
||||
private:
|
||||
bool UpdatePartPositions();
|
||||
int CalculateThumbSize(int track_size);
|
||||
int CalculateThumbPosition(int thumb_size, int track_size);
|
||||
Rect CreateRect(int start, int end);
|
||||
void InvokeScrollEvent(std::function<void()>* event_ptr);
|
||||
void OnTimerExpired();
|
||||
|
||||
bool vertical = false;
|
||||
int scroll_min = 0;
|
||||
int scroll_max = 1;
|
||||
int line_step = 1;
|
||||
int page_step = 10;
|
||||
int position = 0;
|
||||
|
||||
enum MouseDownMode
|
||||
{
|
||||
mouse_down_none,
|
||||
mouse_down_button_decr,
|
||||
mouse_down_button_incr,
|
||||
mouse_down_track_decr,
|
||||
mouse_down_track_incr,
|
||||
mouse_down_thumb_drag
|
||||
} mouse_down_mode = mouse_down_none;
|
||||
|
||||
int thumb_start_position = 0;
|
||||
Point mouse_drag_start_pos;
|
||||
int thumb_start_pixel_position = 0;
|
||||
|
||||
Timer* mouse_down_timer = nullptr;
|
||||
int last_step_size = 0;
|
||||
|
||||
Rect rect_button_decrement;
|
||||
Rect rect_track_decrement;
|
||||
Rect rect_thumb;
|
||||
Rect rect_track_increment;
|
||||
Rect rect_button_increment;
|
||||
|
||||
std::function<void()>* FuncScrollOnMouseDown = nullptr;
|
||||
|
||||
static const int decr_height = 16;
|
||||
static const int incr_height = 16;
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class LineEdit;
|
||||
|
||||
class Statusbar : public Widget
|
||||
{
|
||||
public:
|
||||
Statusbar(Widget* parent);
|
||||
~Statusbar();
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
|
||||
private:
|
||||
LineEdit* CommandEdit = nullptr;
|
||||
};
|
||||
156
libraries/ZWidget/include/zwidget/widgets/textedit/textedit.h
Normal file
156
libraries/ZWidget/include/zwidget/widgets/textedit/textedit.h
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
#include "../../core/timer.h"
|
||||
#include "../../core/span_layout.h"
|
||||
#include "../../core/font.h"
|
||||
#include <functional>
|
||||
|
||||
class Scrollbar;
|
||||
|
||||
class TextEdit : public Widget
|
||||
{
|
||||
public:
|
||||
TextEdit(Widget* parent);
|
||||
~TextEdit();
|
||||
|
||||
bool IsReadOnly() const;
|
||||
bool IsLowercase() const;
|
||||
bool IsUppercase() const;
|
||||
int GetMaxLength() const;
|
||||
std::string GetText() const;
|
||||
int GetLineCount() const;
|
||||
std::string GetLineText(int line) const;
|
||||
std::string GetSelection() const;
|
||||
int GetSelectionStart() const;
|
||||
int GetSelectionLength() const;
|
||||
int GetCursorPos() const;
|
||||
int GetCursorLineNumber() const;
|
||||
double GetTotalHeight();
|
||||
|
||||
void SetSelectAllOnFocusGain(bool enable);
|
||||
void SelectAll();
|
||||
void SetReadOnly(bool enable = true);
|
||||
void SetLowercase(bool enable = true);
|
||||
void SetUppercase(bool enable = true);
|
||||
void SetMaxLength(int length);
|
||||
void SetText(const std::string& text);
|
||||
void AddText(const std::string& text);
|
||||
void SetSelection(int pos, int length);
|
||||
void ClearSelection();
|
||||
void SetCursorPos(int pos);
|
||||
void DeleteSelectedText();
|
||||
void SetInputMask(const std::string& mask);
|
||||
void SetCursorDrawingEnabled(bool enable);
|
||||
|
||||
std::function<std::string(std::string text)> FuncFilterKeyChar;
|
||||
std::function<void()> FuncBeforeEditChanged;
|
||||
std::function<void()> FuncAfterEditChanged;
|
||||
std::function<void()> FuncSelectionChanged;
|
||||
std::function<void()> FuncFocusGained;
|
||||
std::function<void()> FuncFocusLost;
|
||||
std::function<void()> FuncEnterPressed;
|
||||
|
||||
protected:
|
||||
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;
|
||||
void OnKeyChar(std::string chars) override;
|
||||
void OnKeyDown(EInputKey key) override;
|
||||
void OnKeyUp(EInputKey key) override;
|
||||
void OnGeometryChanged() override;
|
||||
void OnEnableChanged() override;
|
||||
void OnSetFocus() override;
|
||||
void OnLostFocus() override;
|
||||
|
||||
private:
|
||||
void LayoutLines(Canvas* canvas);
|
||||
|
||||
void OnTimerExpired();
|
||||
void OnScrollTimerExpired();
|
||||
void CreateComponents();
|
||||
void OnVerticalScroll();
|
||||
void UpdateVerticalScroll();
|
||||
void MoveVerticalScroll();
|
||||
double GetTotalLineHeight();
|
||||
|
||||
struct Line
|
||||
{
|
||||
std::string text;
|
||||
SpanLayout layout;
|
||||
Rect box;
|
||||
bool invalidated = true;
|
||||
};
|
||||
|
||||
struct ivec2
|
||||
{
|
||||
ivec2() = default;
|
||||
ivec2(int x, int y) : x(x), y(y) { }
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
|
||||
bool operator==(const ivec2& b) const { return x == b.x && y == b.y; }
|
||||
bool operator!=(const ivec2& b) const { return x != b.x || y != b.y; }
|
||||
};
|
||||
|
||||
Scrollbar* vert_scrollbar;
|
||||
Timer* timer = nullptr;
|
||||
std::vector<Line> lines = { Line() };
|
||||
ivec2 cursor_pos = { 0, 0 };
|
||||
int max_length = -1;
|
||||
bool mouse_selecting = false;
|
||||
bool lowercase = false;
|
||||
bool uppercase = false;
|
||||
bool readonly = false;
|
||||
ivec2 selection_start = { -1, 0 };
|
||||
int selection_length = 0;
|
||||
std::string input_mask;
|
||||
|
||||
static std::string break_characters;
|
||||
|
||||
void Move(int steps, bool shift, bool ctrl);
|
||||
void InsertText(ivec2 pos, const std::string& str);
|
||||
void Backspace();
|
||||
void Del();
|
||||
ivec2 GetCharacterIndex(Point mouse_wincoords);
|
||||
ivec2 FindNextBreakCharacter(ivec2 pos);
|
||||
ivec2 FindPreviousBreakCharacter(ivec2 pos);
|
||||
bool InputMaskAcceptsInput(ivec2 cursor_pos, const std::string& str);
|
||||
|
||||
std::string::size_type ToOffset(ivec2 pos) const;
|
||||
ivec2 FromOffset(std::string::size_type offset) const;
|
||||
|
||||
VerticalTextPosition vertical_text_align;
|
||||
Timer* scroll_timer = nullptr;
|
||||
|
||||
bool mouse_moves_left = false;
|
||||
bool cursor_blink_visible = true;
|
||||
unsigned int blink_timer = 0;
|
||||
int clip_start_offset = 0;
|
||||
int clip_end_offset = 0;
|
||||
bool ignore_mouse_events = false;
|
||||
|
||||
struct UndoInfo
|
||||
{
|
||||
/* set undo text when:
|
||||
- added char after moving
|
||||
- destructive block operation (del, cut etc)
|
||||
- beginning erase
|
||||
*/
|
||||
|
||||
std::string undo_text;
|
||||
bool first_erase = false;
|
||||
bool first_text_insert = false;
|
||||
} undo_info;
|
||||
|
||||
bool select_all_on_focus_gain = false;
|
||||
|
||||
std::shared_ptr<Font> font = Font::Create("Segoe UI", 12.0);
|
||||
|
||||
template<typename T>
|
||||
static T clamp(T val, T minval, T maxval) { return std::max<T>(std::min<T>(val, maxval), minval); }
|
||||
};
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class TextLabel : public Widget
|
||||
{
|
||||
public:
|
||||
TextLabel(Widget* parent = nullptr);
|
||||
|
||||
void SetText(const std::string& value);
|
||||
const std::string& GetText() const;
|
||||
|
||||
double GetPreferredHeight() const;
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
|
||||
private:
|
||||
std::string text;
|
||||
};
|
||||
11
libraries/ZWidget/include/zwidget/widgets/toolbar/toolbar.h
Normal file
11
libraries/ZWidget/include/zwidget/widgets/toolbar/toolbar.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class Toolbar : public Widget
|
||||
{
|
||||
public:
|
||||
Toolbar(Widget* parent);
|
||||
~Toolbar();
|
||||
};
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "../../core/widget.h"
|
||||
|
||||
class ToolbarButton : public Widget
|
||||
{
|
||||
public:
|
||||
ToolbarButton(Widget* parent);
|
||||
~ToolbarButton();
|
||||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue