cd_lines.e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. end
  120. end
  121. set_line_style_continuous
  122. local
  123. i: INTEGER
  124. do
  125. i := int_canvas_line_style (cnvs, 0)
  126. end
  127. set_line_style_dashed
  128. local
  129. i: INTEGER
  130. do
  131. i := int_canvas_line_style (cnvs, 1)
  132. end
  133. set_line_style_dotted
  134. local
  135. i: INTEGER
  136. do
  137. i := int_canvas_line_style (cnvs, 2)
  138. end
  139. set_line_style_dash_dot
  140. local
  141. i: INTEGER
  142. do
  143. i := int_canvas_line_style (cnvs, 3)
  144. end
  145. set_line_style_dash_dot_dot
  146. local
  147. i: INTEGER
  148. do
  149. i := int_canvas_line_style (cnvs, 4)
  150. end
  151. set_line_style_custom
  152. -- set_line_style_dahes feature must be called before to initialize the
  153. -- custom dashes.
  154. local
  155. i: INTEGER
  156. do
  157. i := int_canvas_line_style (cnvs, 5)
  158. end
  159. set_line_style_dashes (dashes: ARRAY[INTEGER])
  160. -- Defines the custom line style dashes. The first value is the length
  161. -- of the first dash, the second value is the length of the first space,
  162. -- and so on. For example: "10 2 5 2" means dash size 10, space size 2,
  163. -- dash size 5, space size 2, and repeats the pattern. Sizes are
  164. -- in pixels.
  165. local
  166. marks: NATIVE_ARRAY[INTEGER]; i: INTEGER
  167. do
  168. marks := marks.calloc(dashes.count)
  169. from
  170. i := dashes.count - 1
  171. until
  172. i < 0
  173. loop
  174. marks.put(dashes.item(i), i)
  175. i := i - 1
  176. end
  177. int_canvas_line_style_dashes (cnvs, marks.to_external, dashes.count)
  178. end
  179. set_line_width (width: INTEGER)
  180. -- Configures the width of the current line (in pixels).
  181. -- Default value: 1. Valid width interval: >= 1.
  182. require
  183. width > 0
  184. local
  185. i: INTEGER
  186. do
  187. i := int_canvas_line_width (cnvs, width)
  188. end
  189. set_wd_line_width (width: REAL_64)
  190. -- Configures the current line width in millimeters. Valid width
  191. -- interval: >= 1.
  192. require
  193. width > 0
  194. local
  195. i: REAL_64
  196. do
  197. i := int_wd_canvas_line_width (cnvs, width)
  198. end
  199. get_line_width: INTEGER
  200. do
  201. Result := int_canvas_line_width (cnvs, -1)
  202. end
  203. get_wd_line_width: REAL_64
  204. do
  205. Result := int_wd_canvas_line_width (cnvs, -1)
  206. end
  207. set_line_join (style: STRING)
  208. -- Configures the current line style for: CD_MITER, CD_BEVEL or
  209. -- CD_ROUND. Default value: CD_MITER.
  210. require
  211. is_valid_line_join (style)
  212. local
  213. i: INTEGER
  214. do
  215. if style.is_equal("CD_MITER") then
  216. i := int_canvas_line_join (cnvs, 0)
  217. elseif style.is_equal("CD_BEVEL") then
  218. i := int_canvas_line_join (cnvs, 1)
  219. elseif style.is_equal("CD_ROUND") then
  220. i := int_canvas_line_join (cnvs, 2)
  221. end
  222. end
  223. get_line_join: STRING
  224. local
  225. i: INTEGER
  226. do
  227. i := int_canvas_line_join (cnvs, -1)
  228. if i.is_equal(0) then
  229. Result := "CD_MITER"
  230. elseif i.is_equal(1) then
  231. Result := "CD_BEVEL"
  232. elseif i.is_equal(2) then
  233. Result := "CD_ROUND"
  234. end
  235. end
  236. set_line_join_miter
  237. local
  238. i: INTEGER
  239. do
  240. i := int_canvas_line_join (cnvs, 0)
  241. end
  242. set_line_join_bevel
  243. local
  244. i: INTEGER
  245. do
  246. i := int_canvas_line_join (cnvs, 1)
  247. end
  248. set_line_join_round
  249. local
  250. i: INTEGER
  251. do
  252. i := int_canvas_line_join (cnvs, 2)
  253. end
  254. set_line_cap (style: STRING)
  255. -- Configures the current line style for: CD_CAPFLAT, CD_CAPSQUARE or
  256. -- CD_CAPROUND. Default value: CD_CAPFLAT.
  257. require
  258. is_valid_line_cap (style)
  259. local
  260. i: INTEGER
  261. do
  262. if style.is_equal("CD_CAPFLAT") then
  263. i := int_canvas_line_cap (cnvs, 0)
  264. elseif style.is_equal("CD_CAPSQUARE") then
  265. i := int_canvas_line_cap (cnvs, 1)
  266. elseif style.is_equal("CD_CAPROUND") then
  267. i := int_canvas_line_cap (cnvs, 2)
  268. end
  269. end
  270. get_line_cap: STRING
  271. local
  272. i: INTEGER
  273. do
  274. i := int_canvas_line_cap (cnvs, -1)
  275. if i.is_equal(0) then
  276. Result := "CD_CAPFLAT"
  277. elseif i.is_equal(1) then
  278. Result := "CD_CAPSQUARE"
  279. elseif i.is_equal(2) then
  280. Result := "CD_CAPROUND"
  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. feature {}
  302. -- Verifications
  303. is_valid_line_style (style: STRING): BOOLEAN
  304. do
  305. if style.is_equal("CD_CONTINUOUS") or
  306. style.is_equal("CD_DASHED") or
  307. style.is_equal("CD_DOTTED") or
  308. style.is_equal("CD_DASH_DOT") or
  309. style.is_equal("CD_DASH_DOT_DOT") or
  310. style.is_equal("CD_CUSTOM") then
  311. Result := True
  312. else
  313. Result := False
  314. end
  315. end
  316. is_valid_line_join (style: STRING): BOOLEAN
  317. do
  318. if style.is_equal("CD_MITER") or
  319. style.is_equal("CD_BEVEL") or
  320. style.is_equal("CD_ROUND") then
  321. Result := True
  322. else
  323. Result := False
  324. end
  325. end
  326. is_valid_line_cap (style: STRING): BOOLEAN
  327. do
  328. if style.is_equal("CD_CAPFLAT") or
  329. style.is_equal("CD_CAPSQUARE") or
  330. style.is_equal("CD_CAPROUND") then
  331. Result := True
  332. else
  333. Result := False
  334. end
  335. end
  336. -- Internals
  337. int_canvas_line (wgt: POINTER; x1, y1, x2, y2: INTEGER)
  338. external "plug_in"
  339. alias "{
  340. location: "${sys}/plugins"
  341. module_name: "iup"
  342. feature_name: "cdCanvasLine"
  343. }"
  344. end
  345. int_canvas_c_double_line (wgt: POINTER; x1, y1, x2, y2: REAL_64)
  346. external "plug_in"
  347. alias "{
  348. location: "${sys}/plugins"
  349. module_name: "iup"
  350. feature_name: "cdfCanvasLine"
  351. }"
  352. end
  353. int_wd_canvas_line (wgt: POINTER; x1, y1, x2, y2: REAL_64)
  354. external "plug_in"
  355. alias "{
  356. location: "${sys}/plugins"
  357. module_name: "iup"
  358. feature_name: "wdCanvasLine"
  359. }"
  360. end
  361. int_canvas_rect (wgt: POINTER; x1, x2, y1, y2: INTEGER)
  362. external "plug_in"
  363. alias "{
  364. location: "${sys}/plugins"
  365. module_name: "iup"
  366. feature_name: "cdCanvasRect"
  367. }"
  368. end
  369. int_canvas_c_double_rect (wgt: POINTER; x1, x2, y1, y2: REAL_64)
  370. external "plug_in"
  371. alias "{
  372. location: "${sys}/plugins"
  373. module_name: "iup"
  374. feature_name: "cdfCanvasRect"
  375. }"
  376. end
  377. int_wd_canvas_rect (wgt: POINTER; x1, x2, y1, y2: REAL_64)
  378. external "plug_in"
  379. alias "{
  380. location: "${sys}/plugins"
  381. module_name: "iup"
  382. feature_name: "wdCanvasRect"
  383. }"
  384. end
  385. int_canvas_arc (wgt: POINTER; xc, yc, w, h: INTEGER; a1, a2: REAL_64)
  386. external "plug_in"
  387. alias "{
  388. location: "${sys}/plugins"
  389. module_name: "iup"
  390. feature_name: "cdCanvasArc"
  391. }"
  392. end
  393. int_canvas_c_double_arc (wgt: POINTER; xc, yc, w, h, a1, a2: REAL_64)
  394. external "plug_in"
  395. alias "{
  396. location: "${sys}/plugins"
  397. module_name: "iup"
  398. feature_name: "cdfCanvasArc"
  399. }"
  400. end
  401. int_wd_canvas_arc (wgt: POINTER; xc, yc, w, h, a1, a2: REAL_64)
  402. external "plug_in"
  403. alias "{
  404. location: "${sys}/plugins"
  405. module_name: "iup"
  406. feature_name: "wdCanvasArc"
  407. }"
  408. end
  409. int_canvas_line_style (wgt: POINTER; s: INTEGER): INTEGER
  410. external "plug_in"
  411. alias "{
  412. location: "${sys}/plugins"
  413. module_name: "iup"
  414. feature_name: "cdCanvasLineStyle"
  415. }"
  416. end
  417. int_canvas_line_style_dashes (wgt, d: POINTER; c: INTEGER)
  418. external "plug_in"
  419. alias "{
  420. location: "${sys}/plugins"
  421. module_name: "iup"
  422. feature_name: "cdCanvasLineStyleDashes"
  423. }"
  424. end
  425. int_canvas_line_width (wgt: POINTER; w: INTEGER): INTEGER
  426. external "plug_in"
  427. alias "{
  428. location: "${sys}/plugins"
  429. module_name: "iup"
  430. feature_name: "cdCanvasLineWidth"
  431. }"
  432. end
  433. int_wd_canvas_line_width (wgt: POINTER; w: REAL_64): REAL_64
  434. external "plug_in"
  435. alias "{
  436. location: "${sys}/plugins"
  437. module_name: "iup"
  438. feature_name: "wdCanvasLineWidht"
  439. }"
  440. end
  441. int_canvas_line_join (wgt: POINTER; s: INTEGER): INTEGER
  442. external "plug_in"
  443. alias "{
  444. location: "${sys}/plugins"
  445. module_name: "iup"
  446. feature_name: "cdCanvasLineJoin"
  447. }"
  448. end
  449. int_canvas_line_cap (wgt: POINTER; s: INTEGER): INTEGER
  450. external "plug_in"
  451. alias "{
  452. location: "${sys}/plugins"
  453. module_name: "iup"
  454. feature_name: "cdCanvasLineCap"
  455. }"
  456. end
  457. end
  458. -- The MIT License (MIT)
  459. -- Copyright (c) 2016 by German A. Arias
  460. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  461. -- of this software and associated documentation files (the "Software"), to deal
  462. -- in the Software without restriction, including without limitation the rights
  463. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  464. -- copies of the Software, and to permit persons to whom the Software is
  465. -- furnished to do so, subject to the following conditions:
  466. --
  467. -- The above copyright notice and this permission notice shall be included in
  468. -- all copies or substantial portions of the Software.
  469. --
  470. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  471. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  472. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  473. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  474. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  475. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  476. -- SOFTWARE.