PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/nx-3.5.0/nx-X11/lib/Xmu/ShapeWidg.c

#
C | 251 lines | 167 code | 18 blank | 66 comment | 12 complexity | b85981f474f504459891e03b20f814e9 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* $Xorg: ShapeWidg.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
  2. /*
  3. Copyright 1988, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. */
  21. /* $XFree86: xc/lib/Xmu/ShapeWidg.c,v 1.7 2001/01/17 19:42:56 dawes Exp $ */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <X11/IntrinsicP.h>
  26. #include <X11/extensions/shape.h>
  27. #include "Converters.h"
  28. #include "Drawing.h"
  29. #include "Misc.h"
  30. /*
  31. * Prototypes
  32. */
  33. static void ShapeEllipseOrRoundedRectangle(Widget, Bool, int, int);
  34. static void ShapeError(Widget);
  35. static void ShapeOval(Widget);
  36. static void ShapeRectangle(Widget);
  37. /*
  38. * Implementation
  39. */
  40. Boolean
  41. XmuReshapeWidget(Widget w, int shape_style,
  42. int corner_width, int corner_height)
  43. {
  44. switch (shape_style)
  45. {
  46. case XmuShapeRectangle:
  47. ShapeRectangle(w);
  48. break;
  49. case XmuShapeOval:
  50. ShapeOval(w);
  51. break;
  52. case XmuShapeEllipse:
  53. case XmuShapeRoundedRectangle:
  54. ShapeEllipseOrRoundedRectangle(w, shape_style == XmuShapeEllipse,
  55. corner_width, corner_height);
  56. break;
  57. default:
  58. ShapeError(w);
  59. return (False);
  60. }
  61. return (True);
  62. }
  63. static void
  64. ShapeError(Widget w)
  65. {
  66. String params[1];
  67. Cardinal num_params = 1;
  68. params[0] = XtName(w);
  69. XtAppWarningMsg(XtWidgetToApplicationContext(w),
  70. "shapeUnknown", "xmuReshapeWidget", "XmuLibrary",
  71. "Unsupported shape style for Command widget \"%s\"",
  72. params, &num_params);
  73. }
  74. static void
  75. ShapeRectangle(Widget w)
  76. {
  77. XShapeCombineMask(XtDisplay(w), XtWindow(w),
  78. ShapeBounding, 0, 0, None, ShapeSet);
  79. XShapeCombineMask(XtDisplay(w), XtWindow(w),
  80. ShapeClip, 0, 0, None, ShapeSet);
  81. }
  82. /*
  83. * Function:
  84. * ShapeOval
  85. *
  86. * Parameters:
  87. * w - widget to be reshaped
  88. *
  89. * Description:
  90. * Reshapes a widget to a oval format.
  91. *
  92. * Notes:
  93. * X11R6.3 behaviour changed. Now if the height is larger than the
  94. * width, this function inverts the sense of the oval, instead of
  95. * fallbacking to ellipse.
  96. */
  97. static void
  98. ShapeOval(Widget w)
  99. {
  100. Display *dpy = XtDisplay(w);
  101. int width = w->core.width;
  102. int height = w->core.height;
  103. Pixmap p;
  104. XGCValues values;
  105. GC gc;
  106. int rad;
  107. if (width < 3 || height < 3)
  108. return;
  109. width += w->core.border_width << 1;
  110. height += w->core.border_width << 1;
  111. p = XCreatePixmap(dpy, XtWindow(w), width, height, 1);
  112. values.foreground = 0;
  113. values.background = 1;
  114. values.cap_style = CapRound;
  115. values.line_width = Min(width, height);
  116. gc = XCreateGC(dpy, p,
  117. GCForeground | GCBackground | GCLineWidth | GCCapStyle,
  118. &values);
  119. XFillRectangle(dpy, p, gc, 0, 0, width, height);
  120. XSetForeground(dpy, gc, 1);
  121. if (width < height)
  122. {
  123. rad = width >> 1;
  124. XDrawLine(dpy, p, gc, rad, rad, rad, height - rad - 1);
  125. }
  126. else
  127. {
  128. rad = height >> 1;
  129. XDrawLine(dpy, p, gc, rad, rad, width - rad - 1, rad);
  130. }
  131. XShapeCombineMask(dpy, XtWindow(w), ShapeBounding,
  132. -(int)w->core.border_width, -(int)w->core.border_width,
  133. p, ShapeSet);
  134. if (w->core.border_width)
  135. {
  136. XSetForeground(dpy, gc, 0);
  137. XFillRectangle(dpy, p, gc, 0, 0, width, height);
  138. values.line_width = Min(w->core.width, w->core.height);
  139. values.foreground = 1;
  140. XChangeGC(dpy, gc, GCLineWidth | GCForeground, &values);
  141. if (w->core.width < w->core.height)
  142. {
  143. rad = w->core.width >> 1;
  144. XDrawLine(dpy, p, gc, rad, rad, rad, w->core.height - rad - 1);
  145. }
  146. else
  147. {
  148. rad = w->core.height >> 1;
  149. XDrawLine(dpy, p, gc, rad, rad, w->core.width - rad - 1, rad);
  150. }
  151. XShapeCombineMask(dpy, XtWindow(w), ShapeClip, 0, 0, p, ShapeSet);
  152. }
  153. else
  154. XShapeCombineMask(XtDisplay(w), XtWindow(w),
  155. ShapeClip, 0, 0, None, ShapeSet);
  156. XFreePixmap(dpy, p);
  157. XFreeGC(dpy, gc);
  158. }
  159. /*
  160. * Function:
  161. * ShapeEllipseOrRoundedRectangle
  162. *
  163. * Parameters:
  164. * w - widget to be reshaped
  165. * ellipse - True if shape to ellise, rounded rectangle otherwise
  166. * ew - horizontal radius of rounded rectangle
  167. * eh - vertical radius of rouded rectangle
  168. *
  169. * Description:
  170. * Based on the ellipse parameter, gives the widget a elliptical
  171. * shape, or rounded rectangle shape.
  172. *
  173. * Notes:
  174. * The GC is created with a line width of 2, what seens to draw the
  175. * widget border correctly, if the width - height is not proportional.
  176. */
  177. static void
  178. ShapeEllipseOrRoundedRectangle(Widget w, Bool ellipse, int ew, int eh)
  179. {
  180. Display *dpy = XtDisplay(w);
  181. unsigned width = w->core.width;
  182. unsigned height = w->core.height;
  183. Pixmap p;
  184. XGCValues values;
  185. GC gc;
  186. unsigned long mask;
  187. if (width < 3 || width < 3)
  188. return;
  189. width += w->core.border_width << 1;
  190. height += w->core.border_width << 1;
  191. mask = GCForeground | GCLineWidth;
  192. p = XCreatePixmap(dpy, XtWindow(w), width, height, 1);
  193. values.foreground = 0;
  194. values.line_width = 2;
  195. gc = XCreateGC(dpy, p, mask, &values);
  196. XFillRectangle(dpy, p, gc, 0, 0, width, height);
  197. XSetForeground(dpy, gc, 1);
  198. if (!ellipse)
  199. XmuFillRoundedRectangle(dpy, p, gc, 1, 1, width - 2, height - 2, ew, eh);
  200. else
  201. {
  202. XDrawArc(dpy, p, gc, 1, 1, width - 2, height - 2, 0, 360 * 64);
  203. XFillArc(dpy, p, gc, 2, 2, width - 4, height - 4, 0, 360 * 64);
  204. }
  205. XShapeCombineMask(dpy, XtWindow(w), ShapeBounding,
  206. -(int)w->core.border_width, -(int)w->core.border_width,
  207. p, ShapeSet);
  208. if (w->core.border_width)
  209. {
  210. XSetForeground(dpy, gc, 0);
  211. XFillRectangle(dpy, p, gc, 0, 0, width, height);
  212. XSetForeground(dpy, gc, 1);
  213. if (!ellipse)
  214. XmuFillRoundedRectangle(dpy, p, gc, 1, 1,
  215. w->core.width - 2, w->core.height - 2,
  216. ew, eh);
  217. else
  218. XFillArc(dpy, p, gc, 0, 0, w->core.width, w->core.height,
  219. 0, 360 * 64);
  220. XShapeCombineMask(dpy, XtWindow(w), ShapeClip, 0, 0, p, ShapeSet);
  221. }
  222. else
  223. XShapeCombineMask(XtDisplay(w), XtWindow(w),
  224. ShapeClip, 0, 0, None, ShapeSet);
  225. XFreePixmap(dpy, p);
  226. XFreeGC(dpy, gc);
  227. }