void HSSDisplayObject::setDWidth(HSSParserNode::p value)
{
    
    if(value->isA(HSSParserNodeTypeKeywordConstant)){
        
    } else {
        this->dWidth = value;
        HSSObservableProperty observedProperty = HSSObservablePropertyWidth;
        if(this->observedWidth != NULL)
        {
            this->observedWidth->removeObserver(this->observedWidthProperty, this);
        }
        HSSContainer::p parentContainer = this->getParent();
        if(parentContainer){
            this->width = this->_setLDProperty(
                                               &HSSDisplayObject::widthChanged,
                                               value,
                                               parentContainer->width,
                                               observedProperty,
                                               parentContainer.get(),
                                               this->observedWidth,
                                               this->observedWidthProperty,
                                               &(parentContainer->getChildren())
                                               );
        } else {
            this->width = this->_setLDProperty(
                                               NULL,
                                               value,
                                               150,
                                               observedProperty,
                                               this,
                                               this->observedWidth,
                                               this->observedWidthProperty,
                                               NULL
                                               );
        }
        
        
        this->notifyObservers(HSSObservablePropertyWidth, &this->width);
        this->setNeedsSurface(true);
        this->setDirty(true);
    }

    
//    //std_log1("setting width of "+this->name+":");
//    this->dWidth = value;
//    HSSParserNodeType nodeType = value->getType();
//    
//    if(observedWidth != NULL)
//    {
//        //std_log1("#################should unsubscribe observer here#########################");
//        observedWidth->removeObserver(HSSObservablePropertyWidth, this);
//    }
//    
//    switch (nodeType) {
//        case HSSParserNodeTypeNumberConstant:
//        {
//            HSSNumberConstant::p widthValue = boost::static_pointer_cast<HSSNumberConstant>(value);
//            this->width = widthValue->getValue();
//            break;
//        }
//            
//        case HSSParserNodeTypePercentageConstant:
//        {
//            HSSPercentageConstant::p widthValue = boost::static_pointer_cast<HSSPercentageConstant>(value);
//            HSSContainer::p parentContainer = this->getParent();
//            if(parentContainer){
//                this->width = widthValue->getValue(parentContainer->width);
//                parentContainer->observe(HSSObservablePropertyWidth, this, new HSSValueChangedCallback<HSSDisplayObject>(this, &HSSDisplayObject::widthChanged));
//                this->observedWidth = parentContainer.get();
//                
//            } else {
//                this->width = widthValue->getValue(this->width);
//            }
//            break;
//        }
//            
//        case HSSParserNodeTypeExpression:
//        {
//            HSSExpression::p widthExpression = boost::static_pointer_cast<HSSExpression>(value);
//            HSSContainer::p parentContainer = this->getParent();
//            if(parentContainer){
//                widthExpression->setPercentageBase(parentContainer->width);
//                widthExpression->setPercentageObserved(HSSObservablePropertyWidth, parentContainer.get());
//                widthExpression->setScope(&(parentContainer->getChildren()));
//                this->width = widthExpression->evaluate();
//                widthExpression->observe(HSSObservablePropertyWidth, this, new HSSValueChangedCallback<HSSDisplayObject>(this, &HSSDisplayObject::widthChanged));
//            } else {
//                widthExpression->setPercentageBase(this->width);
//                widthExpression->setPercentageObserved(HSSObservablePropertyWidth, this);
//                if(this->isA(HSSObjectTypeContainer)){
//                    HSSContainer * thisContainer = static_cast<HSSContainer *>(this);
//                    widthExpression->setScope(&(thisContainer->getChildren()));
//                }
//                this->width = widthExpression->evaluate();
//                widthExpression->observe(HSSObservablePropertyWidth, this, new HSSValueChangedCallback<HSSDisplayObject>(this, &HSSDisplayObject::widthChanged));
//            }
//            break; 
//        }
//            
//        case HSSParserNodeTypeKeywordConstant:
//            
//            break;
//            
//        default:
//            throw "unknown parser node type while setting dWidth";
//            break;
//    }
//    
//    //fprintf(stderr, "%f\n", this->width);
//    this->notifyObservers(HSSObservablePropertyWidth, &this->width);
//    this->setNeedsSurface(true);
//    this->setDirty(true);
}
void HSSDisplayObject::setDHeight(HSSParserNode::p value)
{
    if(value->isA(HSSParserNodeTypeKeywordConstant)){
        
    } else {
        this->dHeight = value;
        HSSObservableProperty observedProperty = HSSObservablePropertyHeight;
        if(this->observedHeight != NULL)
        {
            this->observedHeight->removeObserver(this->observedHeightProperty, this);
            //we're cheating a bit here (maybe FIXME?)
        }
        HSSContainer::p parentContainer = this->getParent();
        if(parentContainer){
            this->height = this->_setLDProperty(
                                                &HSSDisplayObject::heightChanged,
                                                value,
                                                parentContainer->height,
                                                observedProperty,
                                                parentContainer.get(),
                                                this->observedHeight,
                                                this->observedHeightProperty,
                                                &(parentContainer->getChildren())
                                                );
        } else {
            this->height = this->_setLDProperty(
                                                NULL,
                                                value,
                                                150,
                                                observedProperty,
                                                this,
                                                this->observedHeight,
                                                this->observedHeightProperty,
                                                NULL
                                                );
        }
        
        this->notifyObservers(HSSObservablePropertyHeight, &this->height);
        this->setNeedsSurface(true);
        this->setDirty(true);
    }
    
    
    
//    //std_log1("setting height of "+this->name+":");
//    this->dHeight = value;
//    HSSParserNodeType nodeType = value->getType();
//    
//    if(observedHeight != NULL)
//    {
//        observedHeight->removeObserver(HSSObservablePropertyHeight, this);
//    }
//    
//    switch (nodeType) {
//        case HSSParserNodeTypeNumberConstant:
//        {
//            HSSNumberConstant::p heightValue = boost::static_pointer_cast<HSSNumberConstant>(value);
//            this->height = heightValue->getValue();
//            break;
//        }
//            
//        case HSSParserNodeTypePercentageConstant:
//        {
//            HSSPercentageConstant::p heightValue = boost::static_pointer_cast<HSSPercentageConstant>(value);
//            HSSContainer::p parentContainer = this->getParent();
//            if(parentContainer){
//                this->height = heightValue->getValue(parentContainer->height);
//                parentContainer->observe(HSSObservablePropertyHeight, this, new HSSValueChangedCallback<HSSDisplayObject>(this, &HSSDisplayObject::heightChanged));
//                this->observedHeight = parentContainer.get();
//            }
//            break;
//        }
//        
//        case HSSParserNodeTypeExpression:
//        {
//            HSSExpression::p heightExpression = boost::static_pointer_cast<HSSExpression>(value);
//            HSSContainer::p parentContainer = this->getParent();
//            if(parentContainer){
//                heightExpression->setPercentageBase(parentContainer->height);
//                heightExpression->setPercentageObserved(HSSObservablePropertyHeight, parentContainer.get());
//                heightExpression->setScope(&(parentContainer->getChildren()));
//                this->height = heightExpression->evaluate();
//                heightExpression->observe(HSSObservablePropertyHeight, this, new HSSValueChangedCallback<HSSDisplayObject>(this, &HSSDisplayObject::heightChanged));
//            } else {
//                heightExpression->setPercentageBase(this->height);
//                heightExpression->setPercentageObserved(HSSObservablePropertyHeight, this);
//                if(this->isA(HSSObjectTypeContainer)){
//                    HSSContainer * thisContainer = static_cast<HSSContainer *>(this);
//                    heightExpression->setScope(&(thisContainer->getChildren()));
//                }
//                this->height = heightExpression->evaluate();
//                heightExpression->observe(HSSObservablePropertyValue, this, new HSSValueChangedCallback<HSSDisplayObject>(this, &HSSDisplayObject::heightChanged));
//            }
//            break; 
//        }
//            
//        case HSSParserNodeTypeKeywordConstant:
//            
//            break;
//            
//        default:
//            throw "unknown parser node type while setting dHeight";
//            break;
//    }
//    
//    //fprintf(stderr, "%f\n", this->height);
//    this->notifyObservers(HSSObservablePropertyHeight, &this->height);
//    this->setNeedsSurface(true);
//    this->setDirty(true);
}
Пример #3
0
void AXRController::recursiveMatchRulesToDisplayObjects(const HSSRule::p & rule, const std::vector<HSSDisplayObject::p> & scope, HSSContainer::p container, bool applyingInstructions)
{
    HSSInstruction::p instruction = rule->getInstruction();
    if (instruction && applyingInstructions) {
        switch (instruction->getInstructionType()) {
            case HSSNewInstruction:
            {
                std::string elementName = rule->getSelectorChain()->subject()->getElementName();
                unsigned i;
                unsigned argssize = 1;
                HSSParserNode::p argument = instruction->getArgument();
                if(argument){
                    if (argument->isA(HSSParserNodeTypeNumberConstant)) {
                        HSSNumberConstant::p argnum = boost::static_pointer_cast<HSSNumberConstant>(argument);
                        argssize = (int)argnum->getValue();
                    }
                }
                
                for (i=0; i<argssize; i++) {
                    HSSContainer::p newContainer = HSSContainer::p(new HSSContainer());
                    newContainer->setName(elementName);
                    newContainer->setElementName(elementName);;
                    this->add(newContainer);
                    rule->setThisObj(newContainer);
                    newContainer->rulesAdd(rule, (rule->getActiveByDefault() ? HSSRuleStateOn : HSSRuleStateOff ));
                    std_log1("created "+newContainer->getElementName());
                    newContainer->setNeedsRereadRules(true);
                    newContainer->setNeedsSurface(true);
                    newContainer->setDirty(true);
                    unsigned i, size;
                    this->currentContext.push(newContainer);
                    for (i=0, size=rule->childrenSize(); i<size; i++) {
                        const HSSRule::p childRule = rule->childrenGet(i);
                        this->recursiveMatchRulesToDisplayObjects(childRule, newContainer->getChildren(), newContainer, applyingInstructions);
                    }
                    newContainer->setNeedsRereadRules(true);
                    //newContainer->fireEvent(HSSEventTypeLoad);
                    this->currentContext.pop();
                }
                
                break;
            }
                
            case HSSMoveInstruction:
            {
                HSSContainer::p parent = container->getParent();
                if (parent) {
                    std::vector<HSSDisplayObject::p> moveScope = parent->getChildren();
                    this->setSelectorChain(rule->getSelectorChain());
                    std::vector< std::vector<HSSDisplayObject::p> > selection = this->selectHierarchical(moveScope, container);
                    unsigned i, j, k, size, size2, size3;
                    
                    this->currentContext.push(container);
                    //move the children over
                    for (i=0, size=selection.size(); i<size; i++) {
                        std::vector<HSSDisplayObject::p> inner = selection[i];
                        for (j=0, size2=inner.size(); j<size2; j++) {
                            HSSDisplayObject::p theDO = inner[j];
                            if(theDO != container){
                                theDO->removeFromParent();
                                this->add(theDO);
                                rule->setThisObj(theDO);
                                std_log1("moved "+theDO->getElementName());
                                theDO->rulesAdd(rule, (rule->getActiveByDefault() ? HSSRuleStateOn : HSSRuleStateOff ));
                                theDO->setNeedsRereadRules(true);
                                theDO->setNeedsSurface(true);
                                theDO->setDirty(true);
                                
                                if(theDO->isA(HSSObjectTypeContainer)){
                                    HSSContainer::p theContainer = boost::static_pointer_cast<HSSContainer>(theDO);
                                    this->currentContext.push(theContainer);
                                    //assign more rules
                                    for (k=0, size3=rule->childrenSize(); k<size3; k++) {
                                        const HSSRule::p childRule = rule->childrenGet(k);
                                        this->recursiveMatchRulesToDisplayObjects(childRule, theContainer->getChildren(), theContainer, applyingInstructions);
                                    }
                                    this->currentContext.pop();
                                }
                                
                                theDO->setNeedsRereadRules(true);
                                //theDO->fireEvent(HSSEventTypeLoad);
                            }
                            
                        }
                    }
                    this->currentContext.pop();
                }
                
                break;
            }
                
            case HSSDeleteInstruction:
            {
                //select the items to be deleted
                this->setSelectorChain(rule->getSelectorChain());
                std::vector< std::vector<HSSDisplayObject::p> > selection = this->selectHierarchical(scope, container);
                unsigned i, j, size, size2;
                for (i=0, size=selection.size(); i<size; i++) {
                    std::vector<HSSDisplayObject::p> inner = selection[i];
                    for (j=0, size2=inner.size(); j<size2; j++) {
                        inner[j]->removeFromParent();
                    }
                }
                break;
            }
                                
            default:
                break;
        }
        
    } else {
        HSSSelectorChain::p selectorChain = rule->getSelectorChain();
        if(selectorChain){
            this->setSelectorChain(selectorChain);
            
            //if it starts with a combinator, adjust the scope and selector chain
            bool useAdjustedScope = false;
            std::vector<HSSDisplayObject::p> adjustedScope;
            if(this->currentSelectorNode->isA(HSSParserNodeTypeCombinator)){
                useAdjustedScope = true;
                adjustedScope.push_back(container);
                this->currentChain->prepend(HSSSelector::p(new HSSSelector(container->getElementName())));
                this->currentChainSize ++;
                this->currentSelectorNode = this->currentChain->get(0);
            }
            
            //we observe the parent for dom changes
            container->observe(HSSObservablePropertyTreeChange, HSSObservablePropertyValue, rule.get(), new HSSValueChangedCallback<HSSRule>(rule.get(), &HSSRule::treeChanged));
            rule->setObservedTreeChanger(container.get());
            rule->setThisObj(container);
            
            rule->setOriginalScope(scope);
            std::vector< std::vector<HSSDisplayObject::p> > selection = this->selectHierarchical((useAdjustedScope ? adjustedScope : scope), container);
            unsigned i, j, k, size, size2, size3;
            for (i=0, size=selection.size(); i<size; i++) {
                std::vector<HSSDisplayObject::p> inner = selection[i];
                for (j=0, size2=inner.size(); j<size2; j++) {
                    const HSSDisplayObject::p & displayObject = inner[j];
                    std_log1("match "+displayObject->getElementName());
                    displayObject->rulesAdd(rule, (rule->getActiveByDefault() ? HSSRuleStateOn : HSSRuleStateOff ));
                    
                    displayObject->setNeedsRereadRules(true);
                    displayObject->setNeedsSurface(true);
                    displayObject->setDirty(true);
                    
                    //if it is a container it may have children
                    if(displayObject->isA(HSSObjectTypeContainer)){
                        HSSContainer::p selectedContainer = boost::static_pointer_cast<HSSContainer>(displayObject);
                        this->currentContext.push(selectedContainer);
                        for (k=0, size3=rule->childrenSize(); k<size3; k++) {
                            const HSSRule::p childRule = rule->childrenGet(k);
                            this->recursiveMatchRulesToDisplayObjects(childRule, selectedContainer->getChildren(), selectedContainer, applyingInstructions);
                        }
                        this->currentContext.pop();
                    }
                    
                    displayObject->setNeedsRereadRules(true);
                }
            }
        }
    }
}