コード例 #1
0
ファイル: WSlider.C プロジェクト: StevenFarley/wt
void PaintedSlider::updateSliderPosition()
{
  double l = (slider_->orientation() == Horizontal) ? w() : h();
  double pixelsPerUnit = (l - HANDLE_WIDTH) / range();

  double u = ((double)slider_->value() - slider_->minimum()) * pixelsPerUnit;

  if (slider_->orientation() == Horizontal)
    handle_->setOffsets(u, Left);
  else
    handle_->setOffsets(h() - HANDLE_WIDTH - u, Top);
}
コード例 #2
0
ファイル: WSlider.C プロジェクト: StevenFarley/wt
void PaintedSlider::updateState()
{
  bool rtl = WApplication::instance()->layoutDirection() == RightToLeft;

  std::string resourcesURL = WApplication::resourcesUrl();

  Orientation o = slider_->orientation();

  handle_->setStyleClass("handle");

  if (o == Horizontal) {
    handle_->resize(HANDLE_WIDTH, h());
    handle_->setOffsets(0, Top);
  } else {
    handle_->resize(w(), HANDLE_WIDTH);
    handle_->setOffsets(0, Left);
  }

  double l = o == Horizontal ? w() : h();
  double pixelsPerUnit = (l - HANDLE_WIDTH) / range();

  std::string dir;
  if (o == Horizontal)
    dir = rtl ? "right" : "left";
  else
    dir = "top";
  std::string u = (o == Horizontal ? "x" : "y");
  std::string U = (o == Horizontal ? "X" : "Y");
  std::string maxS = boost::lexical_cast<std::string>(l - HANDLE_WIDTH);
  std::string ppU = boost::lexical_cast<std::string>(pixelsPerUnit);
  std::string minimumS = boost::lexical_cast<std::string>(slider_->minimum());
  std::string maximumS = boost::lexical_cast<std::string>(slider_->maximum());

  std::string width = boost::lexical_cast<std::string>(w());
  std::string horizontal = boost::lexical_cast<std::string>(o == Horizontal);

  /*
   * Note: cancelling the mouseDown event prevents the selection behaviour
   */
  std::string mouseDownJS = 
    """obj.setAttribute('down', " WT_CLASS ".widgetCoordinates(obj, event)."
    + u + "); "
    WT_CLASS ".cancelEvent(event);";

  // = 'u' position relative to background, corrected for slider
  std::string computeD =
    ""  "var objh = " + handle_->jsRef() + ","
    ""      "objb = " + jsRef() + ","
    ""      "page_u = WT.pageCoordinates(event)." + u + ","
    ""      "widget_page_u = WT.widgetPageCoordinates(objb)." + u + ","
    ""      "pos = page_u - widget_page_u,"
    ""      "rtl = " + boost::lexical_cast<std::string>(rtl) + ","
    ""      "horizontal = " + horizontal + ";"
    ""  "if (rtl && horizontal)"
    ""  "  pos = " + width + " - pos;"
    ""  "var d = pos - down;";
  

  std::string mouseMovedJS = 
    """var down = obj.getAttribute('down');"
    """var WT = " WT_CLASS ";"
    """if (down != null && down != '') {"
    + computeD +
    ""  "d = Math.max(0, Math.min(d, " + maxS + "));"
    ""  "var v = Math.round(d/" + ppU + ");"
    ""  "var intd = v*" + ppU + ";"
    ""  "if (Math.abs(WT.pxself(objh, '" + dir + "') - intd) > 1) {"
    ""    "objh.style." + dir + " = intd + 'px';" +
    slider_->sliderMoved().createCall(o == Horizontal ? "v + " + minimumS
				      : maximumS + " - v") + 
    ""  "}"
    """}";

  std::string mouseUpJS = 
    """var down = obj.getAttribute('down');"
    """var WT = " WT_CLASS ";"
    """if (down != null && down != '') {"
    + computeD +
    """d += " + boost::lexical_cast<std::string>(HANDLE_WIDTH / 2) + ";" +
    sliderReleased_.createCall("d") + 
    ""  "obj.removeAttribute('down');"
    """}";

  bool enabled = !slider_->isDisabled();
  
  mouseDownJS_.setJavaScript(std::string("function(obj, event) {") 
			     + (enabled ? mouseDownJS : "") 
			     + "}");
  mouseMovedJS_.setJavaScript(std::string("function(obj, event) {") 
			      + (enabled ? mouseMovedJS : "") 
			      + "}");
  mouseUpJS_.setJavaScript(std::string("function(obj, event) {") 
			   + (enabled ? mouseUpJS : "") 
			   + "}");

  update();
  updateSliderPosition();
}