cd_lines.e 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. deferred class CD_LINES
  2. inherit
  3. CANVAS_DRAW
  4. feature {ANY}
  5. -- Operations
  6. draws_line (x1, y1, x2, y2: INTEGER)
  7. -- Draws a line from (x1,y1) to (x2,y2) using the current foreground
  8. -- color and line width and style. Both points are included in the line.
  9. do
  10. int_canvas_line (cnvs, x1, y1, x2, y2)
  11. end
  12. draws_line_real (x1, y1, x2, y2: REAL_64)
  13. -- As "draws_line" but with REAL_64 coordinates.
  14. do
  15. int_canvas_c_double_line (cnvs, x1, y1, x2, y2)
  16. end
  17. wd_draws_line (x1, y1, x2, y2: REAL_64)
  18. -- As "draws_line" but with World coordinates.
  19. do
  20. int_wd_canvas_line (cnvs, x1, y1, x2, y2)
  21. end
  22. draws_rect (xmin, xmax, ymin, ymax: INTEGER)
  23. -- Draws a rectangle with no filling. All points in the limits of
  24. -- interval x_min<=x<=x_max, y_min<=y<=y_max will be painted. It is
  25. -- affected by line attributes and the foreground color. If the active
  26. -- driver does not include this primitive, it will be simulated using
  27. -- the draws_line primitive.
  28. do
  29. int_canvas_rect (cnvs, xmin, xmax, ymin, ymax)
  30. end
  31. draws_rect_real (xmin, xmax, ymin, ymax: REAL_64)
  32. -- As "draws_rect" but with REAL_64 coordinates.
  33. do
  34. int_canvas_c_double_rect (cnvs, xmin, xmax, ymin, ymax)
  35. end
  36. wd_draws_rect (xmin, xmax, ymin, ymax: REAL_64)
  37. -- As "draws_rect" but with World coordinates.
  38. do
  39. int_wd_canvas_rect (cnvs, xmin, xmax, ymin, ymax)
  40. end
  41. draws_arc (xc, yc, w, h: INTEGER; angle1, angle2: REAL_64)
  42. -- Draws the arc of an ellipse aligned with the axis, using the current
  43. -- foreground color and line width and style.
  44. --
  45. -- The coordinate (xc,yc) defines the center of the ellipse. Dimensions w
  46. -- and h define the elliptic axes X and Y, respectively.
  47. --
  48. -- Angles angle1 and angle2 are in degrees and oriented counter-clockwise.
  49. -- They define the arc start and end, but they are not the angle relative
  50. -- to the center, except when w=h and the ellipse is reduced to a circle.
  51. -- The arc starts at the point
  52. -- (xc+(w/2)*cos(angle1), yc+(h/2)*sin(angle1)) and ends at
  53. -- (xc+(w/2)*cos(angle2), yc+(h/2)*sin(angle2)).
  54. -- A complete ellipse can be drawn using 0 and 360 as the angles. If
  55. -- angle2 is less than angle1 it will be increased by 360 until it is
  56. -- greater than angle1.
  57. --
  58. -- The angles are specified so if the size of the ellipse (w x h) is
  59. -- changed, its shape is preserved. So the angles relative to the center
  60. -- are dependent from the ellipse size. The actual angle can be obtained
  61. -- using rangle = atan2((h/2)*sin(angle), (w/2)*cos(angle)).
  62. do
  63. int_canvas_arc (cnvs, xc, yc, w, h, angle1, angle2)
  64. end
  65. draws_arc_real (xc, yc, w, h, angle1, angle2: REAL_64)
  66. -- As "draws_rect" but with REAL_64 coordinates.
  67. do
  68. int_canvas_c_double_arc (cnvs, xc, yc, w, h, angle1, angle2)
  69. end
  70. wd_draws_arc (xc, yc, w, h, angle1, angle2: REAL_64)
  71. -- As "draws_rect" but with World coordinates.
  72. do
  73. int_wd_canvas_arc (cnvs, xc, yc, w, h, angle1, angle2)
  74. end
  75. -- Attributes
  76. set_line_style (style: STRING)
  77. -- Configures the current line style for: CD_CONTINUOUS, CD_DASHED,
  78. -- CD_DOTTED, CD_DASH_DOT, CD_DASH_DOT_DOT, or CD_CUSTOM.
  79. -- Default value: CD_CONTINUOUS. When CD_CUSTOM is used the
  80. -- set_line_style_dahes feature must be called before to initialize the
  81. -- custom dashes. The spaces are drawn with the background color, except
  82. -- when back opacity is transparent then the background is left unchanged.
  83. require
  84. is_valid_line_style (style)
  85. local
  86. i: INTEGER
  87. do
  88. if style.is_equal("CD_CONTINUOUS") then
  89. i := int_canvas_line_style (cnvs, 0)
  90. elseif style.is_equal("CD_DASHED") then
  91. i := int_canvas_line_style (cnvs, 1)
  92. elseif style.is_equal("CD_DOTTED") then
  93. i := int_canvas_line_style (cnvs, 2)
  94. elseif style.is_equal("CD_DASH_DOT") then
  95. i := int_canvas_line_style (cnvs, 3)
  96. elseif style.is_equal("CD_DASH_DOT_DOT") then
  97. i := int_canvas_line_style (cnvs, 4)
  98. elseif style.is_equal("CD_CUSTOM") then
  99. i := int_canvas_line_style (cnvs, 5)
  100. end
  101. end
  102. get_line_style: STRING
  103. local
  104. i: INTEGER
  105. do
  106. i := int_canvas_line_style (cnvs, -1)
  107. if i.is_equal(0) then
  108. Result := "CD_CONTINUOUS"
  109. elseif i.is_equal(1) then
  110. Result := "CD_DASHED"
  111. elseif i.is_equal(2) then
  112. Result := "CD_DOTTED"
  113. elseif i.is_equal(3) then
  114. Result := "CD_DASH_DOT"
  115. elseif i.is_equal(4) then
  116. Result := "CD_DASH_DOT_DOT"
  117. elseif i.is_equal(5) then
  118. Result := "CD_CUSTOM"
  119. else
  120. Result := "CD_CONTINUOUS"
  121. end
  122. end
  123. set_line_style_continuous
  124. local
  125. i: INTEGER
  126. do
  127. i := int_canvas_line_style (cnvs, 0)
  128. end
  129. set_line_style_dashed
  130. local
  131. i: INTEGER
  132. do
  133. i := int_canvas_line_style (cnvs, 1)
  134. end
  135. set_line_style_dotted
  136. local
  137. i: INTEGER
  138. do
  139. i := int_canvas_line_style (cnvs, 2)
  140. end
  141. set_line_style_dash_dot
  142. local
  143. i: INTEGER
  144. do
  145. i := int_canvas_line_style (cnvs, 3)
  146. end
  147. set_line_style_dash_dot_dot
  148. local
  149. i: INTEGER
  150. do
  151. i := int_canvas_line_style (cnvs, 4)
  152. end
  153. set_line_style_custom
  154. -- set_line_style_dahes feature must be called before to initialize the
  155. -- custom dashes.
  156. local
  157. i: INTEGER
  158. do
  159. i := int_canvas_line_style (cnvs, 5)
  160. end
  161. set_line_style_dashes (dashes: ARRAY[INTEGER])
  162. -- Defines the custom line style dashes. The first value is the length
  163. -- of the first dash, the second value is the length of the first space,
  164. -- and so on. For example: "10 2 5 2" means dash size 10, space size 2,
  165. -- dash size 5, space size 2, and repeats the pattern. Sizes are
  166. -- in pixels.
  167. -- Revise GOBO
  168. local
  169. a: ARRAY[INTEGER]
  170. do
  171. create a.make_from_array(dashes)
  172. a.grow(1)
  173. int_canvas_line_style_dashes (cnvs, get_pointer(a.to_c), dashes.count)
  174. end
  175. set_line_width (width: INTEGER)
  176. -- Configures the width of the current line (in pixels).
  177. -- Default value: 1. Valid width interval: >= 1.
  178. require
  179. width > 0
  180. local
  181. i: INTEGER
  182. do
  183. i := int_canvas_line_width (cnvs, width)
  184. end
  185. set_wd_line_width (width: REAL_64)
  186. -- Configures the current line width in millimeters. Valid width
  187. -- interval: >= 1.
  188. require
  189. width > 0
  190. local
  191. i: REAL_64
  192. do
  193. i := int_wd_canvas_line_width (cnvs, width)
  194. end
  195. get_line_width: INTEGER
  196. do
  197. Result := int_canvas_line_width (cnvs, -1)
  198. end
  199. get_wd_line_width: REAL_64
  200. do
  201. Result := int_wd_canvas_line_width (cnvs, -1)
  202. end
  203. set_line_join (style: STRING)
  204. -- Configures the current line style for: CD_MITER, CD_BEVEL or
  205. -- CD_ROUND. Default value: CD_MITER.
  206. require
  207. is_valid_line_join (style)
  208. local
  209. i: INTEGER
  210. do
  211. if style.is_equal("CD_MITER") then
  212. i := int_canvas_line_join (cnvs, 0)
  213. elseif style.is_equal("CD_BEVEL") then
  214. i := int_canvas_line_join (cnvs, 1)
  215. elseif style.is_equal("CD_ROUND") then
  216. i := int_canvas_line_join (cnvs, 2)
  217. end
  218. end
  219. get_line_join: STRING
  220. local
  221. i: INTEGER
  222. do
  223. i := int_canvas_line_join (cnvs, -1)
  224. if i.is_equal(0) then
  225. Result := "CD_MITER"
  226. elseif i.is_equal(1) then
  227. Result := "CD_BEVEL"
  228. elseif i.is_equal(2) then
  229. Result := "CD_ROUND"
  230. else
  231. Result := "CD_MITER"
  232. end
  233. end
  234. set_line_join_miter
  235. local
  236. i: INTEGER
  237. do
  238. i := int_canvas_line_join (cnvs, 0)
  239. end
  240. set_line_join_bevel
  241. local
  242. i: INTEGER
  243. do
  244. i := int_canvas_line_join (cnvs, 1)
  245. end
  246. set_line_join_round
  247. local
  248. i: INTEGER
  249. do
  250. i := int_canvas_line_join (cnvs, 2)
  251. end
  252. set_line_cap (style: STRING)
  253. -- Configures the current line style for: CD_CAPFLAT, CD_CAPSQUARE or
  254. -- CD_CAPROUND. Default value: CD_CAPFLAT.
  255. require
  256. is_valid_line_cap (style)
  257. local
  258. i: INTEGER
  259. do
  260. if style.is_equal("CD_CAPFLAT") then
  261. i := int_canvas_line_cap (cnvs, 0)
  262. elseif style.is_equal("CD_CAPSQUARE") then
  263. i := int_canvas_line_cap (cnvs, 1)
  264. elseif style.is_equal("CD_CAPROUND") then
  265. i := int_canvas_line_cap (cnvs, 2)
  266. end
  267. end
  268. get_line_cap: STRING
  269. local
  270. i: INTEGER
  271. do
  272. i := int_canvas_line_cap (cnvs, -1)
  273. if i.is_equal(0) then
  274. Result := "CD_CAPFLAT"
  275. elseif i.is_equal(1) then
  276. Result := "CD_CAPSQUARE"
  277. elseif i.is_equal(2) then
  278. Result := "CD_CAPROUND"
  279. else
  280. Result := "CD_CAPFLAT"
  281. end
  282. end
  283. set_line_cap_flat
  284. local
  285. i: INTEGER
  286. do
  287. i := int_canvas_line_cap (cnvs, 0)
  288. end
  289. set_line_cap_square
  290. local
  291. i: INTEGER
  292. do
  293. i := int_canvas_line_cap (cnvs, 1)
  294. end
  295. set_line_cap_round
  296. local
  297. i: INTEGER
  298. do
  299. i := int_canvas_line_cap (cnvs, 2)
  300. end
  301. -- Verifications
  302. is_valid_line_style (style: STRING): BOOLEAN
  303. do
  304. if style.is_equal("CD_CONTINUOUS") or
  305. style.is_equal("CD_DASHED") or
  306. style.is_equal("CD_DOTTED") or
  307. style.is_equal("CD_DASH_DOT") or
  308. style.is_equal("CD_DASH_DOT_DOT") or
  309. style.is_equal("CD_CUSTOM") then
  310. Result := True
  311. else
  312. Result := False
  313. end
  314. end
  315. is_valid_line_join (style: STRING): BOOLEAN
  316. do
  317. if style.is_equal("CD_MITER") or
  318. style.is_equal("CD_BEVEL") or
  319. style.is_equal("CD_ROUND") then
  320. Result := True
  321. else
  322. Result := False
  323. end
  324. end
  325. is_valid_line_cap (style: STRING): BOOLEAN
  326. do
  327. if style.is_equal("CD_CAPFLAT") or
  328. style.is_equal("CD_CAPSQUARE") or
  329. style.is_equal("CD_CAPROUND") then
  330. Result := True
  331. else
  332. Result := False
  333. end
  334. end
  335. feature {NONE}
  336. -- Internals
  337. int_canvas_line (wgt: POINTER; x1, y1, x2, y2: INTEGER)
  338. external
  339. "C inline use %"eiffel-iup.h%""
  340. alias
  341. "cdCanvasLine ($wgt, $x1, $y1, $x2, $y2);"
  342. end
  343. int_canvas_c_double_line (wgt: POINTER; x1, y1, x2, y2: REAL_64)
  344. external
  345. "C inline use %"eiffel-iup.h%""
  346. alias
  347. "cdfCanvasLine ($wgt, $x1, $y1, $x2, $y2);"
  348. end
  349. int_wd_canvas_line (wgt: POINTER; x1, y1, x2, y2: REAL_64)
  350. external
  351. "C inline use %"eiffel-iup.h%""
  352. alias
  353. "wdCanvasLine ($wgt, $x1, $y1, $x2, $y2);"
  354. end
  355. int_canvas_rect (wgt: POINTER; x1, x2, y1, y2: INTEGER)
  356. external
  357. "C inline use %"eiffel-iup.h%""
  358. alias
  359. "cdCanvasRect ($wgt, $x1, $x2, $y1, $y2);"
  360. end
  361. int_canvas_c_double_rect (wgt: POINTER; x1, x2, y1, y2: REAL_64)
  362. external
  363. "C inline use %"eiffel-iup.h%""
  364. alias
  365. "cdfCanvasRect ($wgt, $x1, $x2, $y1, $y2);"
  366. end
  367. int_wd_canvas_rect (wgt: POINTER; x1, x2, y1, y2: REAL_64)
  368. external
  369. "C inline use %"eiffel-iup.h%""
  370. alias
  371. "wdCanvasRect ($wgt, $x1, $x2, $y1, $y2);"
  372. end
  373. int_canvas_arc (wgt: POINTER; xc, yc, w, h: INTEGER; a1, a2: REAL_64)
  374. external
  375. "C inline use %"eiffel-iup.h%""
  376. alias
  377. "cdCanvasArc ($wgt, $xc, $yc, $w, $h, $a1, $a2);"
  378. end
  379. int_canvas_c_double_arc (wgt: POINTER; xc, yc, w, h, a1, a2: REAL_64)
  380. external
  381. "C inline use %"eiffel-iup.h%""
  382. alias
  383. "cdfCanvasArc ($wgt, $xc, $yc, $w, $h, $a1, $a2);"
  384. end
  385. int_wd_canvas_arc (wgt: POINTER; xc, yc, w, h, a1, a2: REAL_64)
  386. external
  387. "C inline use %"eiffel-iup.h%""
  388. alias
  389. "wdCanvasArc ($wgt, $xc, $yc, $w, $h, $a1, $a2);"
  390. end
  391. int_canvas_line_style (wgt: POINTER; s: INTEGER): INTEGER
  392. external
  393. "C inline use %"eiffel-iup.h%""
  394. alias
  395. "return cdCanvasLineStyle ($wgt, $s);"
  396. end
  397. int_canvas_line_style_dashes (wgt, d: POINTER; c: INTEGER)
  398. external
  399. "C inline use %"eiffel-iup.h%""
  400. alias
  401. "cdCanvasLineStyleDashes ($wgt, $d, $c);"
  402. end
  403. int_canvas_line_width (wgt: POINTER; w: INTEGER): INTEGER
  404. external
  405. "C inline use %"eiffel-iup.h%""
  406. alias
  407. "return cdCanvasLineWidth ($wgt, $w);"
  408. end
  409. int_wd_canvas_line_width (wgt: POINTER; w: REAL_64): REAL_64
  410. external
  411. "C inline use %"eiffel-iup.h%""
  412. alias
  413. "return wdCanvasLineWidth ($wgt, $w);"
  414. end
  415. int_canvas_line_join (wgt: POINTER; s: INTEGER): INTEGER
  416. external
  417. "C inline use %"eiffel-iup.h%""
  418. alias
  419. "return cdCanvasLineJoin ($wgt, $s);"
  420. end
  421. int_canvas_line_cap (wgt: POINTER; s: INTEGER): INTEGER
  422. external
  423. "C inline use %"eiffel-iup.h%""
  424. alias
  425. "return cdCanvasLineCap ($wgt, $s);"
  426. end
  427. end
  428. -- The MIT License (MIT)
  429. -- Copyright (c) 2016, 2019, 2020 by German A. Arias
  430. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  431. -- of this software and associated documentation files (the "Software"), to deal
  432. -- in the Software without restriction, including without limitation the rights
  433. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  434. -- copies of the Software, and to permit persons to whom the Software is
  435. -- furnished to do so, subject to the following conditions:
  436. --
  437. -- The above copyright notice and this permission notice shall be included in
  438. -- all copies or substantial portions of the Software.
  439. --
  440. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  441. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  442. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  443. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  444. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  445. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  446. -- SOFTWARE.