mxRectangleShape.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxRectangleShape
  7. *
  8. * Extends <mxShape> to implement a rectangle shape.
  9. * This shape is registered under <mxConstants.SHAPE_RECTANGLE>
  10. * in <mxCellRenderer>.
  11. *
  12. * Constructor: mxRectangleShape
  13. *
  14. * Constructs a new rectangle shape.
  15. *
  16. * Parameters:
  17. *
  18. * bounds - <mxRectangle> that defines the bounds. This is stored in
  19. * <mxShape.bounds>.
  20. * fill - String that defines the fill color. This is stored in <fill>.
  21. * stroke - String that defines the stroke color. This is stored in <stroke>.
  22. * strokewidth - Optional integer that defines the stroke width. Default is
  23. * 1. This is stored in <strokewidth>.
  24. */
  25. function mxRectangleShape(bounds, fill, stroke, strokewidth)
  26. {
  27. mxShape.call(this);
  28. this.bounds = bounds;
  29. this.fill = fill;
  30. this.stroke = stroke;
  31. this.strokewidth = (strokewidth != null) ? strokewidth : 1;
  32. };
  33. /**
  34. * Extends mxShape.
  35. */
  36. mxUtils.extend(mxRectangleShape, mxShape);
  37. /**
  38. * Function: isHtmlAllowed
  39. *
  40. * Returns true for non-rounded, non-rotated shapes with no glass gradient.
  41. */
  42. mxRectangleShape.prototype.isHtmlAllowed = function()
  43. {
  44. var events = true;
  45. if (this.style != null)
  46. {
  47. events = mxUtils.getValue(this.style, mxConstants.STYLE_POINTER_EVENTS, '1') == '1';
  48. }
  49. return !this.isRounded && !this.glass && this.rotation == 0 && (events ||
  50. (this.fill != null && this.fill != mxConstants.NONE));
  51. };
  52. /**
  53. * Function: paintBackground
  54. *
  55. * Generic background painting implementation.
  56. */
  57. mxRectangleShape.prototype.paintBackground = function(c, x, y, w, h)
  58. {
  59. if (this.isRounded)
  60. {
  61. var r = 0;
  62. if (mxUtils.getValue(this.style, mxConstants.STYLE_ABSOLUTE_ARCSIZE, 0) == '1')
  63. {
  64. r = Math.min(w / 2, Math.min(h / 2, mxUtils.getValue(this.style,
  65. mxConstants.STYLE_ARCSIZE, mxConstants.LINE_ARCSIZE) / 2));
  66. }
  67. else
  68. {
  69. var f = mxUtils.getValue(this.style, mxConstants.STYLE_ARCSIZE,
  70. mxConstants.RECTANGLE_ROUNDING_FACTOR * 100) / 100;
  71. r = Math.min(w * f, h * f);
  72. }
  73. c.roundrect(x, y, w, h, r, r);
  74. }
  75. else
  76. {
  77. c.rect(x, y, w, h);
  78. }
  79. c.fillAndStroke();
  80. };
  81. /**
  82. * Function: isRoundable
  83. *
  84. * Adds roundable support.
  85. */
  86. mxRectangleShape.prototype.isRoundable = function()
  87. {
  88. return true;
  89. };
  90. /**
  91. * Function: paintForeground
  92. *
  93. * Generic background painting implementation.
  94. */
  95. mxRectangleShape.prototype.paintForeground = function(c, x, y, w, h)
  96. {
  97. if (this.glass && !this.outline && this.fill != null && this.fill != mxConstants.NONE)
  98. {
  99. this.paintGlassEffect(c, x, y, w, h, this.getArcSize(w + this.strokewidth, h + this.strokewidth));
  100. }
  101. };