#ifndef COMMON_WIDGETS_H
#define COMMON_WIDGETS_H
#include "Widget.h"
#include "Property.h"
#include "Containers.h"
#include "Namespace.h"
BEGIN_NAMESPACE
// Generally this doesn't end up being too useful right now
// What does it buy you?
// The property is the thing being interacted with, not the widget class
template <typename T>
class AbstractValueWidget : public Widget
{
public:
AbstractValueWidget(Widget* a_parent, AbstractProperty<T>& a_value)
: Widget(a_parent, false, 0, 0, 10, 10), value(a_value)
{
connect(value.valueChanged, this, &AbstractValueWidget<T>::refresh);
}
void refresh(Variant& newValue)
{
repaint();
}
// In this case, we store and expose an AbstractProperty since the value held here is used
// to reflect some state from somewhere else in the program
AbstractProperty<T>& value;
};
// checked and value are now synonym for the same value
// A bit of overhead in having the extra reference
class CheckableWidget : public AbstractValueWidget<bool>
{
public:
CheckableWidget(Widget* a_parent, AbstractProperty<bool>& a_value);
AbstractProperty<bool>& checked;
};
// Some ideas about generating meta-info for reflection and introspection
#ifndef DECLARE_WIDGET
# define DECLARE_WIDGET(decl) decl;
#endif
#ifndef WIDGET_CLASS
# define WIDGET_CLASS(cls) class cls
#endif
DECLARE_WIDGET
(
class HBox : public Widget
{
public:
HBox(Widget* a_parent);
}
);
WIDGET_CLASS(VBox) : public Widget
{
public:
VBox(Widget* a_parent);
};
class GroupBoxSpacer : public Widget
{
public:
GroupBoxSpacer(Widget* a_parent);
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
};
class GroupBox : public AbstractValueWidget<String>
{
public:
GroupBox(Widget* a_parent, AbstractProperty<String>& a_title);
AbstractProperty<String>& title;
Property<bool> disabled;
protected:
void paintEvent(PaintEvent& a_event);
private:
GroupBoxSpacer m_titleSpace;
};
class HSpace : public Widget
{
public:
HSpace(Widget* a_parent);
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
};
class VSpace : public Widget
{
public:
VSpace(Widget* a_parent);
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
};
class Label : public AbstractValueWidget<String>
{
public:
Label(Widget* a_parent, AbstractProperty<String>& a_text);
Property<bool> disabled;
// In this case, we store and expose an AbstractProperty since the value held here is used
// to reflect some state from somewhere else in the program
AbstractProperty<String>& text;
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
};
class Button : public Widget
{
public:
Button(Widget* a_parent, const char* a_text);
Signal activated;
Property<String> text;
Property<bool> disabled;
Property<bool> hasFocus;
Property<bool> isDefault;
protected:
void refresh(Variant& newValue);
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
void mouseEnterEvent(MouseEvent& a_event);
void mouseLeaveEvent(MouseEvent& a_event);
void mouseEvent(MouseEvent& a_event);
private:
bool m_mouseDownInButton;
bool m_mouseOverButton;
};
class CheckBox : public CheckableWidget
{
public:
CheckBox(Widget* a_parent, AbstractProperty<bool>& a_checked, const char* a_text);
virtual void toggle();
Property<String> text;
Property<bool> disabled;
Property<bool> hasFocus;
Property<bool> isDefault;
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
void mouseEnterEvent(MouseEvent& a_event);
void mouseLeaveEvent(MouseEvent& a_event);
void mouseEvent(MouseEvent& a_event);
private:
bool m_mouseDownInButton;
bool m_mouseOverButton;
};
class RadioButton : public CheckableWidget
{
public:
RadioButton(Widget* a_parent, AbstractProperty<bool>& a_checked, const char* a_text);
virtual void toggle();
Property<String> text;
Property<bool> disabled;
Property<bool> hasFocus;
Property<bool> isDefault;
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
void mouseEnterEvent(MouseEvent& a_event);
void mouseLeaveEvent(MouseEvent& a_event);
void mouseEvent(MouseEvent& a_event);
private:
bool m_mouseDownInButton;
bool m_mouseOverButton;
};
class LineEdit : public AbstractValueWidget<String>
{
public:
LineEdit(Widget* a_parent, AbstractProperty<String>& a_text);
AbstractProperty<String>& text;
Property<bool> disabled;
Property<bool> hasFocus;
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
void timerEvent(TimerEvent& a_event);
void keyEvent(KeyEvent& a_event);
void paintEvent(PaintEvent& a_event);
void mouseEnterEvent(MouseEvent& a_event);
void mouseLeaveEvent(MouseEvent& a_event);
void mouseEvent(MouseEvent& a_event);
private:
bool m_mouseOver;
TimerId m_timerId;
bool m_carrotOn;
int m_carrotPosition;
};
class Slider : public AbstractValueWidget<int>
{
public:
Slider(Widget* a_parent, AbstractProperty<int>& a_sliderValue);
Property<bool> disabled;
Property<bool> hasFocus;
protected:
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
void mouseLeaveEvent(MouseEvent& a_event);
void mouseEvent(MouseEvent& a_event);
private:
bool m_mouseOverSliderHandle;
bool m_mouseDownInSliderHandle;
Rectangle getSliderHandleRectangle();
};
class ComboBox : public AbstractValueWidget<int>
{
public:
ComboBox(Widget* a_parent, AbstractProperty<int>& a_value);
Signal activated;
Property<String> text;
Property<bool> disabled;
Property<bool> hasFocus;
Property<bool> isDefault;
AbstractProperty<int>& currentIndex;
void addItem(String a_itemText);
protected:
void refresh(Variant& newValue);
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
void mouseEnterEvent(MouseEvent& a_event);
void mouseLeaveEvent(MouseEvent& a_event);
void mouseEvent(MouseEvent& a_event);
void keyEvent(KeyEvent& a_event);
private:
bool m_mouseDownInButton;
bool m_mouseOverButton;
// Option List should be a property - a property list
Vector<String> m_items;
};
class ProgressBar : public AbstractValueWidget<int>
{
public:
ProgressBar(Widget* a_parent, AbstractProperty<int>& a_value);
Property<bool> disabled;
AbstractProperty<int>& progress;
protected:
void refresh(Variant& newValue);
void sizeOptions(SizeOptions& a_sizeOptions);
void paintEvent(PaintEvent& a_event);
};
END_NAMESPACE
#endif // COMMON_WIDGETS_H