add new style states (first,middle,last,alternate)

This commit is contained in:
Eduardo Bart 2012-01-02 20:49:34 -02:00
parent 41c2d18b93
commit 7fc4ebd89e
5 changed files with 162 additions and 111 deletions

View File

@ -266,11 +266,10 @@ namespace Fw
DisabledState = 16,
CheckedState = 32,
OnState = 64,
LastState = 128
//FirstState,
//MiddleState,
//LastState,
//AlternateState
FirstState = 128,
MiddleState = 256,
AlternateState = 512,
LastState = 1024,
};
enum AppicationFlags {

View File

@ -83,6 +83,10 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("isFocused", &UIWidget::isFocused);
g_lua.bindClassMemberFunction<UIWidget>("isHovered", &UIWidget::isHovered);
g_lua.bindClassMemberFunction<UIWidget>("isPressed", &UIWidget::isPressed);
g_lua.bindClassMemberFunction<UIWidget>("isFirst", &UIWidget::isFirst);
g_lua.bindClassMemberFunction<UIWidget>("isMiddle", &UIWidget::isMiddle);
g_lua.bindClassMemberFunction<UIWidget>("isLast", &UIWidget::isLast);
g_lua.bindClassMemberFunction<UIWidget>("isAlternate", &UIWidget::isAlternate);
g_lua.bindClassMemberFunction<UIWidget>("isVisible", &UIWidget::isVisible);
g_lua.bindClassMemberFunction<UIWidget>("isHidden", &UIWidget::isHidden);
g_lua.bindClassMemberFunction<UIWidget>("isExplicitlyEnabled", &UIWidget::isExplicitlyEnabled);
@ -142,11 +146,6 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("getChildIndex", &UIWidget::getChildIndex);
g_lua.bindClassMemberFunction<UIWidget>("updateParentLayout", &UIWidget::updateParentLayout);
g_lua.bindClassMemberFunction<UIWidget>("updateLayout", &UIWidget::updateLayout);
g_lua.bindClassMemberFunction<UIWidget>("updateStates", &UIWidget::updateStates);
g_lua.bindClassMemberFunction<UIWidget>("updateState", &UIWidget::updateState);
g_lua.bindClassMemberFunction<UIWidget>("setState", &UIWidget::setState);
g_lua.bindClassMemberFunction<UIWidget>("hasState", &UIWidget::hasState);
g_lua.bindClassMemberFunction<UIWidget>("updateStyle", &UIWidget::updateStyle);
g_lua.bindClassMemberFunction<UIWidget>("applyStyle", &UIWidget::applyStyle);
// UILayout

View File

@ -70,7 +70,7 @@ Fw::AnchorEdge Fw::translateAnchorEdge(std::string anchorEdge)
Fw::WidgetState Fw::translateState(std::string state)
{
boost::to_lower(state);
boost::erase_all(state, " ");
boost::trim(state);
if(state == "active")
return Fw::ActiveState;
else if(state == "focus")
@ -85,6 +85,14 @@ Fw::WidgetState Fw::translateState(std::string state)
return Fw::DisabledState;
else if(state == "on")
return Fw::OnState;
else if(state == "first")
return Fw::FirstState;
else if(state == "middle")
return Fw::MiddleState;
else if(state == "last")
return Fw::LastState;
else if(state == "alternate")
return Fw::AlternateState;
else
return Fw::InvalidState;
}

View File

@ -422,6 +422,7 @@ void UIWidget::addChild(const UIWidgetPtr& child)
// update new child states
child->updateStates();
updateChildrenIndexStates();
}
void UIWidget::insertChild(int index, const UIWidgetPtr& child)
@ -454,6 +455,7 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child)
// update new child states
child->updateStates();
updateChildrenIndexStates();
}
void UIWidget::removeChild(const UIWidgetPtr& child)
@ -481,6 +483,7 @@ void UIWidget::removeChild(const UIWidgetPtr& child)
// update child states
child->updateStates();
updateChildrenIndexStates();
if(focusAnother && !m_focusedChild)
focusPreviousChild(Fw::ActiveFocusReason);
@ -549,6 +552,7 @@ void UIWidget::moveChildToTop(const UIWidgetPtr& child)
assert(it != m_children.end());
m_children.erase(it);
m_children.push_back(child);
updateChildrenIndexStates();
}
void UIWidget::moveChildToIndex(const UIWidgetPtr& child, int index)
@ -561,6 +565,7 @@ void UIWidget::moveChildToIndex(const UIWidgetPtr& child, int index)
assert(it != m_children.end());
m_children.erase(it);
m_children.insert(m_children.begin() + index - 1, child);
updateChildrenIndexStates();
}
void UIWidget::lockChild(const UIWidgetPtr& child)
@ -663,6 +668,16 @@ void UIWidget::updateLayout()
m_layout->update();
}
void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
{
try {
onStyleApply(styleNode);
callLuaField("onStyleApply", styleNode);
} catch(Exception& e) {
logError("Failed to apply style to widget '", m_id, "' style: ", e.what());
}
}
bool UIWidget::setState(Fw::WidgetState state, bool on)
{
if(state == Fw::InvalidState)
@ -694,7 +709,8 @@ void UIWidget::updateState(Fw::WidgetState state)
bool oldStatus = hasState(state);
bool updateChildren = false;
if(state == Fw::ActiveState) {
switch(state) {
case Fw::ActiveState: {
UIWidgetPtr widget = asUIWidget();
UIWidgetPtr parent;
do {
@ -707,11 +723,13 @@ void UIWidget::updateState(Fw::WidgetState state)
} while(widget = parent);
updateChildren = true;
break;
}
else if(state == Fw::FocusState) {
case Fw::FocusState: {
newStatus = (getParent() && getParent()->getFocusedChild() == asUIWidget());
break;
}
else if(state == Fw::HoverState) {
case Fw::HoverState: {
updateChildren = true;
Point mousePos = g_window.getMousePos();
UIWidgetPtr widget = asUIWidget();
@ -724,11 +742,13 @@ void UIWidget::updateState(Fw::WidgetState state)
break;
}
} while(widget = parent);
break;
}
else if(state == Fw::PressedState) {
case Fw::PressedState: {
newStatus = m_pressed;
break;
}
else if(state == Fw::DisabledState) {
case Fw::DisabledState: {
bool enabled = true;
updateChildren = true;
UIWidgetPtr widget = asUIWidget();
@ -739,8 +759,25 @@ void UIWidget::updateState(Fw::WidgetState state)
}
} while(widget = widget->getParent());
newStatus = !enabled;
break;
}
else {
case Fw::FirstState: {
newStatus = (getParent() && getParent()->getFirstChild() == asUIWidget());
break;
}
case Fw::MiddleState: {
newStatus = (getParent() && getParent()->getFirstChild() != asUIWidget() && getParent()->getLastChild() != asUIWidget());
break;
}
case Fw::LastState: {
newStatus = (getParent() && getParent()->getLastChild() == asUIWidget());
break;
}
case Fw::AlternateState: {
newStatus = (getParent() && (getParent()->getChildIndex(asUIWidget()) % 2) == 1);
break;
}
default:
return;
}
@ -765,6 +802,16 @@ void UIWidget::updateStates()
updateState((Fw::WidgetState)state);
}
void UIWidget::updateChildrenIndexStates()
{
for(const UIWidgetPtr& child : m_children) {
child->updateState(Fw::FirstState);
child->updateState(Fw::MiddleState);
child->updateState(Fw::LastState);
child->updateState(Fw::AlternateState);
}
}
void UIWidget::updateStyle()
{
if(!m_style)
@ -814,16 +861,6 @@ void UIWidget::updateStyle()
m_stateStyle = newStateStyle;
}
void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
{
try {
onStyleApply(styleNode);
callLuaField("onStyleApply", styleNode);
} catch(Exception& e) {
logError("Failed to apply style to widget '", m_id, "' style: ", e.what());
}
}
void UIWidget::onStyleApply(const OTMLNodePtr& styleNode)
{
// first set id

View File

@ -90,6 +90,10 @@ public:
bool isFocused() { return hasState(Fw::FocusState); }
bool isHovered() { return hasState(Fw::HoverState); }
bool isPressed() { return hasState(Fw::PressedState); }
bool isFirst() { return hasState(Fw::FirstState); }
bool isMiddle() { return hasState(Fw::MiddleState); }
bool isLast() { return hasState(Fw::LastState); }
bool isAlternate() { return hasState(Fw::AlternateState); }
bool isVisible();
bool isHidden() { return !isVisible(); }
bool isExplicitlyEnabled() { return m_enabled; }
@ -153,17 +157,21 @@ public:
void updateParentLayout();
void updateLayout();
void updateStates();
virtual void updateState(Fw::WidgetState state);
bool setState(Fw::WidgetState state, bool on);
bool hasState(Fw::WidgetState state);
void updateStyle();
void applyStyle(const OTMLNodePtr& styleNode);
UIWidgetPtr asUIWidget() { return std::static_pointer_cast<UIWidget>(shared_from_this()); }
protected:
bool setState(Fw::WidgetState state, bool on);
bool hasState(Fw::WidgetState state);
private:
void updateState(Fw::WidgetState state);
void updateStates();
void updateChildrenIndexStates();
void updateStyle();
protected:
/// Triggered when widget style is changed
virtual void onStyleApply(const OTMLNodePtr& styleNode);