You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

800 lines
24KB

  1. //
  2. // "$Id: fl_rect.cxx 8630 2011-05-01 12:45:29Z AlbrechtS $"
  3. //
  4. // Rectangle drawing routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. /**
  28. \file fl_rect.cxx
  29. \brief Drawing and clipping routines for rectangles.
  30. */
  31. // These routines from fl_draw.H are used by the standard boxtypes
  32. // and thus are always linked into an fltk program.
  33. // Also all fl_clip routines, since they are always linked in so
  34. // that minimal update works.
  35. #include <config.h>
  36. #include <FL/Fl.H>
  37. #include <FL/Fl_Widget.H>
  38. #include <FL/Fl_Printer.H>
  39. #include <FL/fl_draw.H>
  40. #include <FL/x.H>
  41. // fl_line_width_ must contain the absolute value of the current
  42. // line width to be used for X11 clipping (see below).
  43. // This is defined in src/fl_line_style.cxx
  44. extern int fl_line_width_;
  45. #ifdef __APPLE_QUARTZ__
  46. extern float fl_quartz_line_width_;
  47. #define USINGQUARTZPRINTER (Fl_Surface_Device::surface()->class_name() == Fl_Printer::class_id)
  48. #endif
  49. #ifdef USE_X11
  50. #ifndef SHRT_MAX
  51. #define SHRT_MAX (32767)
  52. #endif
  53. /*
  54. We need to check some coordinates for areas for clipping before we
  55. use X functions, because X can't handle coordinates outside the 16-bit
  56. range. Since all windows use relative coordinates > 0, we do also
  57. check for negative values. X11 only, see also STR #2304.
  58. Note that this is only necessary for large objects, where only a
  59. part of the object is visible. The draw() functions (e.g. box
  60. drawing) must be clipped correctly. This is usually only a matter
  61. for large container widgets. The individual child widgets will be
  62. clipped completely.
  63. We define the usable X coordinate space as [ -LW : SHRT_MAX - LW ]
  64. where LW = current line width for drawing. This is done so that
  65. horizontal and vertical line drawing works correctly, even in real
  66. border cases, e.g. drawing a rectangle slightly outside the top left
  67. window corner, but with a line width so that a part of the line should
  68. be visible (in this case 2 of 5 pixels):
  69. fl_line_style (FL_SOLID,5); // line width = 5
  70. fl_rect (-1,-1,100,100); // top/left: 2 pixels visible
  71. In this example case, no clipping would be done, because X can
  72. handle it and clip unneeded pixels.
  73. Note that we must also take care of the case where fl_line_width_
  74. is zero (maybe unitialized). If this is the case, we assume a line
  75. width of 1.
  76. Todo: Arbitrary line drawings (e.g. polygons) and clip regions
  77. are not yet done.
  78. Note:
  79. We could use max. screen coordinates instead of SHRT_MAX, but that
  80. would need more work and would probably be slower. We assume that
  81. all window coordinates are >= 0 and that no window extends up to
  82. 32767 - LW (where LW = current line width). Thus it is safe to clip
  83. all coordinates to this range before calling X functions. If this
  84. is not true, then clip_to_short() and clip_x() must be redefined.
  85. It would be somewhat easier if we had fl_clip_w and fl_clip_h, as
  86. defined in FLTK 2.0 (for the upper clipping bounds)...
  87. */
  88. /*
  89. clip_to_short() returns 1, if the area is invisible (clipped),
  90. because ...
  91. (a) w or h are <= 0 i.e. nothing is visible
  92. (b) x+w or y+h are < kmin i.e. left of or above visible area
  93. (c) x or y are > kmax i.e. right of or below visible area
  94. kmin and kmax are the minimal and maximal X coordinate values,
  95. as defined above. In this case x, y, w, and h are not changed.
  96. It returns 0, if the area is potentially visible and X can handle
  97. clipping. x, y, w, and h may have been adjusted to fit into the
  98. X coordinate space.
  99. Use this for clipping rectangles, as used in fl_rect() and
  100. fl_rectf().
  101. */
  102. static int clip_to_short(int &x, int &y, int &w, int &h) {
  103. int lw = (fl_line_width_ > 0) ? fl_line_width_ : 1;
  104. int kmin = -lw;
  105. int kmax = SHRT_MAX - lw;
  106. if (w <= 0 || h <= 0) return 1; // (a)
  107. if (x+w < kmin || y+h < kmin) return 1; // (b)
  108. if (x > kmax || y > kmax) return 1; // (c)
  109. if (x < kmin) { w -= (kmin-x); x = kmin; }
  110. if (y < kmin) { h -= (kmin-y); y = kmin; }
  111. if (x+w > kmax) w = kmax - x;
  112. if (y+h > kmax) h = kmax - y;
  113. return 0;
  114. }
  115. /*
  116. clip_x() returns a coordinate value clipped to the 16-bit coordinate
  117. space (see above). This can be used to draw horizontal and vertical
  118. lines that can be handled by X11. Each single coordinate value can
  119. be clipped individually, and the result can be used directly, e.g.
  120. in fl_xyline() and fl_yxline(). Note that this can't be used for
  121. arbitrary lines (not horizontal or vertical).
  122. */
  123. static int clip_x (int x) {
  124. int lw = (fl_line_width_ > 0) ? fl_line_width_ : 1;
  125. int kmin = -lw;
  126. int kmax = SHRT_MAX - lw;
  127. if (x < kmin)
  128. x = kmin;
  129. else if (x > kmax)
  130. x = kmax;
  131. return x;
  132. }
  133. #endif // USE_X11
  134. void Fl_Graphics_Driver::rect(int x, int y, int w, int h) {
  135. if (w<=0 || h<=0) return;
  136. #if defined(USE_X11)
  137. if (!clip_to_short(x, y, w, h))
  138. XDrawRectangle(fl_display, fl_window, fl_gc, x, y, w-1, h-1);
  139. #elif defined(WIN32)
  140. MoveToEx(fl_gc, x, y, 0L);
  141. LineTo(fl_gc, x+w-1, y);
  142. LineTo(fl_gc, x+w-1, y+h-1);
  143. LineTo(fl_gc, x, y+h-1);
  144. LineTo(fl_gc, x, y);
  145. #elif defined(__APPLE_QUARTZ__)
  146. if ( (!USINGQUARTZPRINTER) && fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  147. CGRect rect = CGRectMake(x, y, w-1, h-1);
  148. CGContextStrokeRect(fl_gc, rect);
  149. if ( (!USINGQUARTZPRINTER) && fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  150. #else
  151. # error unsupported platform
  152. #endif
  153. }
  154. void Fl_Graphics_Driver::rectf(int x, int y, int w, int h) {
  155. if (w<=0 || h<=0) return;
  156. #if defined(USE_X11)
  157. if (!clip_to_short(x, y, w, h))
  158. XFillRectangle(fl_display, fl_window, fl_gc, x, y, w, h);
  159. #elif defined(WIN32)
  160. RECT rect;
  161. rect.left = x; rect.top = y;
  162. rect.right = x + w; rect.bottom = y + h;
  163. FillRect(fl_gc, &rect, fl_brush());
  164. #elif defined(__APPLE_QUARTZ__)
  165. CGFloat delta_size = 0.9;
  166. CGFloat delta_ori = 0;
  167. if (USINGQUARTZPRINTER) {
  168. delta_size = 0;
  169. delta_ori = 0.5;
  170. }
  171. CGRect rect = CGRectMake(x - delta_ori, y - delta_ori, w - delta_size , h - delta_size);
  172. CGContextFillRect(fl_gc, rect);
  173. #else
  174. # error unsupported platform
  175. #endif
  176. }
  177. void Fl_Graphics_Driver::xyline(int x, int y, int x1) {
  178. #if defined(USE_X11)
  179. XDrawLine(fl_display, fl_window, fl_gc, clip_x(x), clip_x(y), clip_x(x1), clip_x(y));
  180. #elif defined(WIN32)
  181. MoveToEx(fl_gc, x, y, 0L); LineTo(fl_gc, x1+1, y);
  182. #elif defined(__APPLE_QUARTZ__)
  183. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  184. CGContextMoveToPoint(fl_gc, x, y);
  185. CGContextAddLineToPoint(fl_gc, x1, y);
  186. CGContextStrokePath(fl_gc);
  187. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  188. #else
  189. # error unsupported platform
  190. #endif
  191. }
  192. void Fl_Graphics_Driver::xyline(int x, int y, int x1, int y2) {
  193. #if defined (USE_X11)
  194. XPoint p[3];
  195. p[0].x = clip_x(x); p[0].y = p[1].y = clip_x(y);
  196. p[1].x = p[2].x = clip_x(x1); p[2].y = clip_x(y2);
  197. XDrawLines(fl_display, fl_window, fl_gc, p, 3, 0);
  198. #elif defined(WIN32)
  199. if (y2 < y) y2--;
  200. else y2++;
  201. MoveToEx(fl_gc, x, y, 0L);
  202. LineTo(fl_gc, x1, y);
  203. LineTo(fl_gc, x1, y2);
  204. #elif defined(__APPLE_QUARTZ__)
  205. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  206. CGContextMoveToPoint(fl_gc, x, y);
  207. CGContextAddLineToPoint(fl_gc, x1, y);
  208. CGContextAddLineToPoint(fl_gc, x1, y2);
  209. CGContextStrokePath(fl_gc);
  210. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  211. #else
  212. #error unsupported platform
  213. #endif
  214. }
  215. void Fl_Graphics_Driver::xyline(int x, int y, int x1, int y2, int x3) {
  216. #if defined(USE_X11)
  217. XPoint p[4];
  218. p[0].x = clip_x(x); p[0].y = p[1].y = clip_x(y);
  219. p[1].x = p[2].x = clip_x(x1); p[2].y = p[3].y = clip_x(y2);
  220. p[3].x = clip_x(x3);
  221. XDrawLines(fl_display, fl_window, fl_gc, p, 4, 0);
  222. #elif defined(WIN32)
  223. if(x3 < x1) x3--;
  224. else x3++;
  225. MoveToEx(fl_gc, x, y, 0L);
  226. LineTo(fl_gc, x1, y);
  227. LineTo(fl_gc, x1, y2);
  228. LineTo(fl_gc, x3, y2);
  229. #elif defined(__APPLE_QUARTZ__)
  230. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  231. CGContextMoveToPoint(fl_gc, x, y);
  232. CGContextAddLineToPoint(fl_gc, x1, y);
  233. CGContextAddLineToPoint(fl_gc, x1, y2);
  234. CGContextAddLineToPoint(fl_gc, x3, y2);
  235. CGContextStrokePath(fl_gc);
  236. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  237. #else
  238. # error unsupported platform
  239. #endif
  240. }
  241. void Fl_Graphics_Driver::yxline(int x, int y, int y1) {
  242. #if defined(USE_X11)
  243. XDrawLine(fl_display, fl_window, fl_gc, clip_x(x), clip_x(y), clip_x(x), clip_x(y1));
  244. #elif defined(WIN32)
  245. if (y1 < y) y1--;
  246. else y1++;
  247. MoveToEx(fl_gc, x, y, 0L); LineTo(fl_gc, x, y1);
  248. #elif defined(__APPLE_QUARTZ__)
  249. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  250. CGContextMoveToPoint(fl_gc, x, y);
  251. CGContextAddLineToPoint(fl_gc, x, y1);
  252. CGContextStrokePath(fl_gc);
  253. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  254. #else
  255. # error unsupported platform
  256. #endif
  257. }
  258. void Fl_Graphics_Driver::yxline(int x, int y, int y1, int x2) {
  259. #if defined(USE_X11)
  260. XPoint p[3];
  261. p[0].x = p[1].x = clip_x(x); p[0].y = clip_x(y);
  262. p[1].y = p[2].y = clip_x(y1); p[2].x = clip_x(x2);
  263. XDrawLines(fl_display, fl_window, fl_gc, p, 3, 0);
  264. #elif defined(WIN32)
  265. if (x2 > x) x2++;
  266. else x2--;
  267. MoveToEx(fl_gc, x, y, 0L);
  268. LineTo(fl_gc, x, y1);
  269. LineTo(fl_gc, x2, y1);
  270. #elif defined(__APPLE_QUARTZ__)
  271. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  272. CGContextMoveToPoint(fl_gc, x, y);
  273. CGContextAddLineToPoint(fl_gc, x, y1);
  274. CGContextAddLineToPoint(fl_gc, x2, y1);
  275. CGContextStrokePath(fl_gc);
  276. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  277. #else
  278. # error unsupported platform
  279. #endif
  280. }
  281. void Fl_Graphics_Driver::yxline(int x, int y, int y1, int x2, int y3) {
  282. #if defined(USE_X11)
  283. XPoint p[4];
  284. p[0].x = p[1].x = clip_x(x); p[0].y = clip_x(y);
  285. p[1].y = p[2].y = clip_x(y1); p[2].x = p[3].x = clip_x(x2);
  286. p[3].y = clip_x(y3);
  287. XDrawLines(fl_display, fl_window, fl_gc, p, 4, 0);
  288. #elif defined(WIN32)
  289. if(y3<y1) y3--;
  290. else y3++;
  291. MoveToEx(fl_gc, x, y, 0L);
  292. LineTo(fl_gc, x, y1);
  293. LineTo(fl_gc, x2, y1);
  294. LineTo(fl_gc, x2, y3);
  295. #elif defined(__APPLE_QUARTZ__)
  296. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  297. CGContextMoveToPoint(fl_gc, x, y);
  298. CGContextAddLineToPoint(fl_gc, x, y1);
  299. CGContextAddLineToPoint(fl_gc, x2, y1);
  300. CGContextAddLineToPoint(fl_gc, x2, y3);
  301. CGContextStrokePath(fl_gc);
  302. if (USINGQUARTZPRINTER || fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  303. #else
  304. # error unsupported platform
  305. #endif
  306. }
  307. void Fl_Graphics_Driver::line(int x, int y, int x1, int y1) {
  308. #if defined(USE_X11)
  309. XDrawLine(fl_display, fl_window, fl_gc, x, y, x1, y1);
  310. #elif defined(WIN32)
  311. MoveToEx(fl_gc, x, y, 0L);
  312. LineTo(fl_gc, x1, y1);
  313. // Draw the last point *again* because the GDI line drawing
  314. // functions will not draw the last point ("it's a feature!"...)
  315. SetPixel(fl_gc, x1, y1, fl_RGB());
  316. #elif defined(__APPLE_QUARTZ__)
  317. if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  318. CGContextMoveToPoint(fl_gc, x, y);
  319. CGContextAddLineToPoint(fl_gc, x1, y1);
  320. CGContextStrokePath(fl_gc);
  321. if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  322. #else
  323. # error unsupported platform
  324. #endif
  325. }
  326. void Fl_Graphics_Driver::line(int x, int y, int x1, int y1, int x2, int y2) {
  327. #if defined(USE_X11)
  328. XPoint p[3];
  329. p[0].x = x; p[0].y = y;
  330. p[1].x = x1; p[1].y = y1;
  331. p[2].x = x2; p[2].y = y2;
  332. XDrawLines(fl_display, fl_window, fl_gc, p, 3, 0);
  333. #elif defined(WIN32)
  334. MoveToEx(fl_gc, x, y, 0L);
  335. LineTo(fl_gc, x1, y1);
  336. LineTo(fl_gc, x2, y2);
  337. // Draw the last point *again* because the GDI line drawing
  338. // functions will not draw the last point ("it's a feature!"...)
  339. SetPixel(fl_gc, x2, y2, fl_RGB());
  340. #elif defined(__APPLE_QUARTZ__)
  341. if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, true);
  342. CGContextMoveToPoint(fl_gc, x, y);
  343. CGContextAddLineToPoint(fl_gc, x1, y1);
  344. CGContextAddLineToPoint(fl_gc, x2, y2);
  345. CGContextStrokePath(fl_gc);
  346. if (fl_quartz_line_width_ > 1.5f) CGContextSetShouldAntialias(fl_gc, false);
  347. #else
  348. # error unsupported platform
  349. #endif
  350. }
  351. void Fl_Graphics_Driver::loop(int x, int y, int x1, int y1, int x2, int y2) {
  352. #if defined(USE_X11)
  353. XPoint p[4];
  354. p[0].x = x; p[0].y = y;
  355. p[1].x = x1; p[1].y = y1;
  356. p[2].x = x2; p[2].y = y2;
  357. p[3].x = x; p[3].y = y;
  358. XDrawLines(fl_display, fl_window, fl_gc, p, 4, 0);
  359. #elif defined(WIN32)
  360. MoveToEx(fl_gc, x, y, 0L);
  361. LineTo(fl_gc, x1, y1);
  362. LineTo(fl_gc, x2, y2);
  363. LineTo(fl_gc, x, y);
  364. #elif defined(__APPLE_QUARTZ__)
  365. CGContextSetShouldAntialias(fl_gc, true);
  366. CGContextMoveToPoint(fl_gc, x, y);
  367. CGContextAddLineToPoint(fl_gc, x1, y1);
  368. CGContextAddLineToPoint(fl_gc, x2, y2);
  369. CGContextClosePath(fl_gc);
  370. CGContextStrokePath(fl_gc);
  371. CGContextSetShouldAntialias(fl_gc, false);
  372. #else
  373. # error unsupported platform
  374. #endif
  375. }
  376. void Fl_Graphics_Driver::loop(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3) {
  377. #if defined(USE_X11)
  378. XPoint p[5];
  379. p[0].x = x; p[0].y = y;
  380. p[1].x = x1; p[1].y = y1;
  381. p[2].x = x2; p[2].y = y2;
  382. p[3].x = x3; p[3].y = y3;
  383. p[4].x = x; p[4].y = y;
  384. XDrawLines(fl_display, fl_window, fl_gc, p, 5, 0);
  385. #elif defined(WIN32)
  386. MoveToEx(fl_gc, x, y, 0L);
  387. LineTo(fl_gc, x1, y1);
  388. LineTo(fl_gc, x2, y2);
  389. LineTo(fl_gc, x3, y3);
  390. LineTo(fl_gc, x, y);
  391. #elif defined(__APPLE_QUARTZ__)
  392. CGContextSetShouldAntialias(fl_gc, true);
  393. CGContextMoveToPoint(fl_gc, x, y);
  394. CGContextAddLineToPoint(fl_gc, x1, y1);
  395. CGContextAddLineToPoint(fl_gc, x2, y2);
  396. CGContextAddLineToPoint(fl_gc, x3, y3);
  397. CGContextClosePath(fl_gc);
  398. CGContextStrokePath(fl_gc);
  399. CGContextSetShouldAntialias(fl_gc, false);
  400. #else
  401. # error unsupported platform
  402. #endif
  403. }
  404. void Fl_Graphics_Driver::polygon(int x, int y, int x1, int y1, int x2, int y2) {
  405. XPoint p[4];
  406. p[0].x = x; p[0].y = y;
  407. p[1].x = x1; p[1].y = y1;
  408. p[2].x = x2; p[2].y = y2;
  409. #if defined (USE_X11)
  410. p[3].x = x; p[3].y = y;
  411. XFillPolygon(fl_display, fl_window, fl_gc, p, 3, Convex, 0);
  412. XDrawLines(fl_display, fl_window, fl_gc, p, 4, 0);
  413. #elif defined(WIN32)
  414. SelectObject(fl_gc, fl_brush());
  415. Polygon(fl_gc, p, 3);
  416. #elif defined(__APPLE_QUARTZ__)
  417. CGContextSetShouldAntialias(fl_gc, true);
  418. CGContextMoveToPoint(fl_gc, x, y);
  419. CGContextAddLineToPoint(fl_gc, x1, y1);
  420. CGContextAddLineToPoint(fl_gc, x2, y2);
  421. CGContextClosePath(fl_gc);
  422. CGContextFillPath(fl_gc);
  423. CGContextSetShouldAntialias(fl_gc, false);
  424. #else
  425. # error unsupported platform
  426. #endif
  427. }
  428. void Fl_Graphics_Driver::polygon(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3) {
  429. XPoint p[5];
  430. p[0].x = x; p[0].y = y;
  431. p[1].x = x1; p[1].y = y1;
  432. p[2].x = x2; p[2].y = y2;
  433. p[3].x = x3; p[3].y = y3;
  434. #if defined(USE_X11)
  435. p[4].x = x; p[4].y = y;
  436. XFillPolygon(fl_display, fl_window, fl_gc, p, 4, Convex, 0);
  437. XDrawLines(fl_display, fl_window, fl_gc, p, 5, 0);
  438. #elif defined(WIN32)
  439. SelectObject(fl_gc, fl_brush());
  440. Polygon(fl_gc, p, 4);
  441. #elif defined(__APPLE_QUARTZ__)
  442. CGContextSetShouldAntialias(fl_gc, true);
  443. CGContextMoveToPoint(fl_gc, x, y);
  444. CGContextAddLineToPoint(fl_gc, x1, y1);
  445. CGContextAddLineToPoint(fl_gc, x2, y2);
  446. CGContextAddLineToPoint(fl_gc, x3, y3);
  447. CGContextClosePath(fl_gc);
  448. CGContextFillPath(fl_gc);
  449. CGContextSetShouldAntialias(fl_gc, false);
  450. #else
  451. # error unsupported platform
  452. #endif
  453. }
  454. void Fl_Graphics_Driver::point(int x, int y) {
  455. #if defined(USE_X11)
  456. XDrawPoint(fl_display, fl_window, fl_gc, clip_x(x), clip_x(y));
  457. #elif defined(WIN32)
  458. SetPixel(fl_gc, x, y, fl_RGB());
  459. #elif defined(__APPLE_QUARTZ__)
  460. CGContextFillRect(fl_gc, CGRectMake(x - 0.5, y - 0.5, 1, 1) );
  461. #else
  462. # error unsupported platform
  463. #endif
  464. }
  465. Region XRegionFromRectangle ( Fl_Region rg )
  466. {
  467. if ( rg )
  468. {
  469. Region region = XCreateRegion();
  470. XRectangle rr;
  471. cairo_rectangle_int_t rect;
  472. cairo_region_get_extents( rg, &rect );
  473. rr.x = rect.x;
  474. rr.y = rect.y;
  475. rr.width = rect.width;
  476. rr.height = rect.height;
  477. XUnionRectWithRegion( &rr, region, region );
  478. return region;
  479. }
  480. return 0;
  481. }
  482. ////////////////////////////////////////////////////////////////
  483. #if !defined(WIN32) && !defined(__APPLE__)
  484. // Missing X call: (is this the fastest way to init a 1-rectangle region?)
  485. // MSWindows equivalent exists, implemented inline in win32.H
  486. Fl_Region XRectangleRegion(int x, int y, int w, int h) {
  487. cairo_rectangle_int_t rect;
  488. rect.x = x;
  489. rect.y = y;
  490. rect.width = w;
  491. rect.height = h;
  492. return cairo_region_create_rectangle( &rect );
  493. }
  494. #endif
  495. void Fl_Graphics_Driver::restore_clip() {
  496. fl_clip_state_number++;
  497. Fl_Region r = rstack[rstackptr];
  498. #if defined(USE_X11)
  499. #elif defined(WIN32)
  500. SelectClipRgn(fl_gc, r); //if r is NULL, clip is automatically cleared
  501. #elif defined(__APPLE_QUARTZ__)
  502. if ( fl_window ) { // clipping for a true window
  503. Fl_X::q_clear_clipping();
  504. Fl_X::q_fill_context();//flip coords if bitmap context
  505. //apply program clip
  506. if (r) {
  507. CGContextClipToRects(fl_gc, r->rects, r->count);
  508. }
  509. } else if (fl_gc) { // clipping for an offscreen drawing world (CGBitmap)
  510. Fl_X::q_clear_clipping();
  511. Fl_X::q_fill_context();
  512. if (r) {
  513. CGContextClipToRects(fl_gc, r->rects, r->count);
  514. }
  515. }
  516. #else
  517. # error unsupported platform
  518. #endif
  519. cairo_t *cr = fl_cairo_context;
  520. if ( cr )
  521. {
  522. cairo_reset_clip( cr );
  523. if ( r )
  524. {
  525. cairo_rectangle_int_t rect;
  526. for ( int i = cairo_region_num_rectangles( r ); --i >= 0; )
  527. {
  528. cairo_region_get_rectangle( r, i, &rect );
  529. cairo_rectangle( cr, rect.x, rect.y, rect.width, rect.height );
  530. }
  531. // cairo_set_source_rgb( cr, 0, 1, 0 );
  532. // cairo_rectangle( cr, r->x(), r->y(), r->w(), r->h() );
  533. // cairo_stroke_preserve( cr );
  534. cairo_clip( cr );
  535. }
  536. }
  537. }
  538. void Fl_Graphics_Driver::clip_region(Fl_Region r) {
  539. Fl_Region oldr = rstack[rstackptr];
  540. if (oldr && r != oldr )
  541. {
  542. cairo_region_destroy( oldr );
  543. }
  544. rstack[rstackptr] = r ? cairo_region_reference( r ) : 0;
  545. fl_restore_clip();
  546. }
  547. Fl_Region Fl_Graphics_Driver::clip_region() {
  548. return rstack[rstackptr];
  549. }
  550. void Fl_Graphics_Driver::push_clip(int x, int y, int w, int h) {
  551. Fl_Region r;
  552. if (w > 0 && h > 0) {
  553. r = XRectangleRegion(x,y,w,h);
  554. Fl_Region current = rstack[rstackptr];
  555. if (current) {
  556. #if defined(USE_X11)
  557. cairo_region_intersect( r, current );
  558. #elif defined(WIN32)
  559. CombineRgn(r,r,current,RGN_AND);
  560. #elif defined(__APPLE_QUARTZ__)
  561. XDestroyRegion(r);
  562. r = Fl_X::intersect_region_and_rect(current, x,y,w,h);
  563. #else
  564. # error unsupported platform
  565. #endif
  566. }
  567. } else { // make empty clip region:
  568. #if defined(USE_X11)
  569. r = XRectangleRegion( 0, 0, 0, 0 );
  570. #elif defined(WIN32)
  571. r = CreateRectRgn(0,0,0,0);
  572. #elif defined(__APPLE_QUARTZ__)
  573. r = XRectangleRegion(0,0,0,0);
  574. #else
  575. # error unsupported platform
  576. #endif
  577. }
  578. if (rstackptr < region_stack_max) rstack[++rstackptr] = r;
  579. else Fl::warning("fl_push_clip: clip stack overflow!\n");
  580. fl_restore_clip();
  581. }
  582. // make there be no clip (used by fl_begin_offscreen() only!)
  583. void Fl_Graphics_Driver::push_no_clip() {
  584. if (rstackptr < region_stack_max) rstack[++rstackptr] = 0;
  585. else Fl::warning("fl_push_no_clip: clip stack overflow!\n");
  586. fl_restore_clip();
  587. }
  588. // pop back to previous clip:
  589. void Fl_Graphics_Driver::pop_clip() {
  590. if (rstackptr > 0) {
  591. Fl_Region oldr = rstack[rstackptr--];
  592. if (oldr)
  593. {
  594. cairo_region_destroy( oldr );
  595. }
  596. } else Fl::warning("fl_pop_clip: clip stack underflow!\n");
  597. fl_restore_clip();
  598. }
  599. int Fl_Graphics_Driver::not_clipped(int x, int y, int w, int h) {
  600. if (x+w <= 0 || y+h <= 0) return 0;
  601. Fl_Region r = rstack[rstackptr];
  602. if (!r) return 1;
  603. #if defined (USE_X11)
  604. // get rid of coordinates outside the 16-bit range the X calls take.
  605. if (clip_to_short(x,y,w,h)) return 0; // clipped
  606. cairo_rectangle_int_t rect;
  607. rect.x = x; rect.y = y; rect.width = w; rect.height = h;
  608. cairo_region_overlap_t o = cairo_region_contains_rectangle( r, &rect );
  609. return o != CAIRO_REGION_OVERLAP_OUT;
  610. #elif defined(WIN32)
  611. RECT rect;
  612. if (Fl_Surface_Device::surface()->class_name() == Fl_Printer::class_id) { // in case of print context, convert coords from logical to device
  613. POINT pt[2] = { {x, y}, {x + w, y + h} };
  614. LPtoDP(fl_gc, pt, 2);
  615. rect.left = pt[0].x; rect.top = pt[0].y; rect.right = pt[1].x; rect.bottom = pt[1].y;
  616. } else {
  617. rect.left = x; rect.top = y; rect.right = x+w; rect.bottom = y+h;
  618. }
  619. return RectInRegion(r,&rect);
  620. #elif defined(__APPLE_QUARTZ__)
  621. CGRect arg = fl_cgrectmake_cocoa(x, y, w, h);
  622. for (int i = 0; i < r->count; i++) {
  623. CGRect test = CGRectIntersection(r->rects[i], arg);
  624. if (!CGRectIsEmpty(test)) return 1;
  625. }
  626. return 0;
  627. #else
  628. # error unsupported platform
  629. #endif
  630. }
  631. // return rectangle surrounding intersection of this rectangle and clip:
  632. int Fl_Graphics_Driver::clip_box(int x, int y, int w, int h, int& X, int& Y, int& W, int& H){
  633. X = x; Y = y; W = w; H = h;
  634. Fl_Region r = rstack[rstackptr];
  635. if (!r) return 0;
  636. #if defined(USE_X11)
  637. cairo_rectangle_int_t rect;
  638. rect.x = x; rect.y = y; rect.width = w; rect.height = h;
  639. cairo_region_t *t = cairo_region_copy( r );
  640. cairo_region_intersect_rectangle( t, &rect );
  641. cairo_region_get_extents( t, &rect );
  642. X = rect.x; Y = rect.y; W = rect.width; H = rect.height;
  643. cairo_region_overlap_t o = cairo_region_contains_rectangle( r, &rect );
  644. cairo_region_destroy(t);
  645. switch ( o )
  646. {
  647. case CAIRO_REGION_OVERLAP_IN:
  648. return 0;
  649. case CAIRO_REGION_OVERLAP_OUT:
  650. return 2;
  651. case CAIRO_REGION_OVERLAP_PART:
  652. return 1;
  653. default:
  654. return 2;
  655. }
  656. #elif defined(WIN32)
  657. // The win32 API makes no distinction between partial and complete
  658. // intersection, so we have to check for partial intersection ourselves.
  659. // However, given that the regions may be composite, we have to do
  660. // some voodoo stuff...
  661. Fl_Region rr = XRectangleRegion(x,y,w,h);
  662. Fl_Region temp = CreateRectRgn(0,0,0,0);
  663. int ret;
  664. if (CombineRgn(temp, rr, r, RGN_AND) == NULLREGION) { // disjoint
  665. W = H = 0;
  666. ret = 2;
  667. } else if (EqualRgn(temp, rr)) { // complete
  668. ret = 0;
  669. } else { // partial intersection
  670. RECT rect;
  671. GetRgnBox(temp, &rect);
  672. if(Fl_Surface_Device::surface()->class_name() == Fl_Printer::class_id) { // if print context, convert coords from device to logical
  673. POINT pt[2] = { {rect.left, rect.top}, {rect.right, rect.bottom} };
  674. DPtoLP(fl_gc, pt, 2);
  675. X = pt[0].x; Y = pt[0].y; W = pt[1].x - X; H = pt[1].y - Y;
  676. }
  677. else {
  678. X = rect.left; Y = rect.top; W = rect.right - X; H = rect.bottom - Y;
  679. }
  680. ret = 1;
  681. }
  682. DeleteObject(temp);
  683. DeleteObject(rr);
  684. return ret;
  685. #elif defined(__APPLE_QUARTZ__)
  686. CGRect arg = fl_cgrectmake_cocoa(x, y, w, h);
  687. CGRect u = CGRectMake(0,0,0,0);
  688. CGRect test;
  689. for(int i = 0; i < r->count; i++) {
  690. test = CGRectIntersection(r->rects[i], arg);
  691. if( ! CGRectIsEmpty(test) ) {
  692. if(CGRectIsEmpty(u)) u = test;
  693. else u = CGRectUnion(u, test);
  694. }
  695. }
  696. X = int(u.origin.x);
  697. Y = int(u.origin.y);
  698. W = int(u.size.width + 1);
  699. H = int(u.size.height + 1);
  700. if(CGRectIsEmpty(u)) W = H = 0;
  701. return ! CGRectEqualToRect(arg, u);
  702. #else
  703. # error unsupported platform
  704. #endif
  705. }
  706. //
  707. // End of "$Id: fl_rect.cxx 8630 2011-05-01 12:45:29Z AlbrechtS $".
  708. //