void PaintedSlider::updateSliderPosition() { double l = (slider_->orientation() == Horizontal) ? w() : h(); double pixelsPerUnit = (l - slider_->handleWidth()) / range(); double u = ((double)slider_->value() - slider_->minimum()) * pixelsPerUnit; if (slider_->orientation() == Horizontal) { handle_->setOffsets(u, Left); fill_->setWidth(u + slider_->handleWidth() / 2); } else { handle_->setOffsets(h() - slider_->handleWidth() - u, Top); fill_->setHeight(u + slider_->handleWidth() / 2); } }
void PaintedSlider::updateState() { bool rtl = WApplication::instance()->layoutDirection() == RightToLeft; Orientation o = slider_->orientation(); if (o == Horizontal) { handle_->resize(slider_->handleWidth(), h()); handle_->setOffsets(0, Top); } else { handle_->resize(w(), slider_->handleWidth()); handle_->setOffsets(0, Left); } double l = o == Horizontal ? w() : h(); double pixelsPerUnit = (l - slider_->handleWidth()) / range(); std::string dir; std::string size; if (o == Horizontal) { dir = rtl ? "right" : "left"; size = "width"; } else { dir = "top"; size = "height"; } char u = (o == Horizontal ? 'x' : 'y'); double max = l - slider_->handleWidth(); bool horizontal = o == Horizontal; /* * Note: cancelling the mouseDown event prevents the selection behaviour */ WStringStream mouseDownJS; mouseDownJS << "obj.setAttribute('down', " WT_CLASS << ".widgetCoordinates(obj, event)." << u << ");" << WT_CLASS ".cancelEvent(event);"; WStringStream computeD; // = 'u' position relative to background, corrected for slider computeD << "var objh = " << handle_->jsRef() << "," << "objf = " << fill_->jsRef() << "," << "objb = " << jsRef() << "," << "page_u = WT.pageCoordinates(event)." << u << "," << "widget_page_u = WT.widgetPageCoordinates(objb)." << u << "," << "pos = page_u - widget_page_u," << "rtl = " << rtl << "," << "horizontal = " << horizontal << ";" << "if (rtl && horizontal)" << "pos = " << l << " - pos;" << "var d = pos - down;"; WStringStream mouseMovedJS; mouseMovedJS << "var down = obj.getAttribute('down');" << "var WT = " WT_CLASS ";" << "if (down != null && down != '') {" << computeD.str() << "d = Math.max(0, Math.min(d, " << max << "));" << "var v = Math.round(d/" << pixelsPerUnit << ");" << "var intd = v*" << pixelsPerUnit << ";" << "if (Math.abs(WT.pxself(objh, '" << dir << "') - intd) > 1) {" << "objf.style." << size << " = "; if (o == Vertical) mouseMovedJS << '(' << max << " - intd + " << (slider_->handleWidth() / 2) << ")"; else mouseMovedJS << "intd + " << (slider_->handleWidth() / 2); mouseMovedJS << " + 'px';" << "objh.style." << dir << " = intd + 'px';" << "var vs = "; if (o == Horizontal) mouseMovedJS << "v + " << slider_->minimum(); else mouseMovedJS << slider_->maximum() << " - v"; mouseMovedJS << ";" << "var f = objb.parentNode.onValueChange;" << "if (f) f(vs);"; if (slider_->sliderMoved().needsUpdate(true)) mouseMovedJS << slider_->sliderMoved().createCall("vs"); mouseMovedJS << "}" << "}"; WStringStream mouseUpJS; mouseUpJS << "var down = obj.getAttribute('down');" << "var WT = " WT_CLASS ";" << "if (down != null && down != '') {" << computeD.str() << "d += " << (slider_->handleWidth() / 2) << ";" << sliderReleased_.createCall("d") << "obj.removeAttribute('down');" << "}"; bool enabled = !slider_->isDisabled(); mouseDownJS_.setJavaScript(std::string("function(obj, event) {") + (enabled ? mouseDownJS.str() : "") + "}"); mouseMovedJS_.setJavaScript(std::string("function(obj, event) {") + (enabled ? mouseMovedJS.str() : "") + "}"); mouseUpJS_.setJavaScript(std::string("function(obj, event) {") + (enabled ? mouseUpJS.str() : "") + "}"); update(); updateSliderPosition(); }