canvas_example.e 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. class CANVAS_EXAMPLE
  2. -- This example show how draw basic primitives at IUP_CANVAS (marks,
  3. -- lines, polygons, filled areas and text).
  4. inherit
  5. IUP_INTERFACE
  6. create {ANY}
  7. make
  8. feature {ANY}
  9. angle: REAL_64
  10. canv: IUP_CANVAS
  11. make
  12. local
  13. gui: IUP
  14. i: STRING
  15. a: ARRAY[IUP_WIDGET]
  16. l: IUP_LABEL
  17. b: IUP_BUTTON
  18. v: IUP_VBOX
  19. w: IUP_DIALOG
  20. do
  21. gui := iup_open
  22. create canv.canvas
  23. canv.set_size(300, 150)
  24. canv.set_cb_map(agent map_canvas)
  25. canv.set_cb_canvas_action(agent draw)
  26. canv.set_cb_button(agent button)
  27. create l.label("First test drawing with IUP_CANVAS!")
  28. create b.button("Rotate 5 degrees")
  29. b.set_cb_action(agent rotate_canvas)
  30. a := {ARRAY[IUP_WIDGET]} << l, b, canv >>
  31. create v.vbox(a)
  32. v.set_alignment("ACENTER")
  33. v.set_gap(10)
  34. v.set_margin(10, 10)
  35. create w.dialog(v)
  36. w.set_title("CD test")
  37. w.set_raster_size(520, 470)
  38. i := w.show
  39. gui.main_loop
  40. gui.close
  41. end
  42. map_canvas (widget: IUP_CANVAS): STRING
  43. -- Create the internal canvas (CD_IUP) when the widget
  44. -- is mapped (IUP_CANVAS)
  45. local
  46. canvas: CD_IUP
  47. do
  48. create canvas.create_double_buffer_with_canvas(widget)
  49. Result := "IUP_DEFAULT"
  50. end
  51. draw (widget: IUP_CANVAS; x, y: REAL_32): STRING
  52. local
  53. ok: BOOLEAN
  54. canvas: CD_IUP
  55. do
  56. canvas := widget.get_cd_canvas
  57. ok := canvas.activate
  58. if ok then
  59. -- Rotate
  60. canvas.apply_transform_rotate(angle)
  61. -- Clear the canvas with a white background
  62. canvas.set_predefined_background_color(canvas.cd_white)
  63. canvas.clear
  64. canvas.set_interior_style_solid
  65. -- Draw two lines
  66. canvas.set_line_width(3)
  67. canvas.set_predefined_foreground_color(canvas.cd_blue)
  68. canvas.draws_line(40, 40, 450, 40)
  69. canvas.draws_line(45, 65, 455, 65)
  70. -- Draw a rect with dotted line
  71. canvas.set_line_style_dotted
  72. canvas.set_predefined_foreground_color(canvas.cd_red)
  73. canvas.draws_rect(60, 200, 80, 175)
  74. -- Draw an arc
  75. canvas.set_line_style_continuous
  76. canvas.draws_arc(150, 200, 100, 50, 0, 150)
  77. -- Draw a box (filled rect)
  78. canvas.set_predefined_foreground_color(canvas.cd_yellow)
  79. canvas.draws_box(240, 470, 80, 175)
  80. -- Draw a sector (filled arc)
  81. canvas.set_predefined_foreground_color(canvas.cd_magenta)
  82. canvas.draws_sector(260, 200, 100, 50, 0, 150)
  83. -- Draw a text
  84. --canvas.set_text_orientation(20)
  85. canvas.draws_text(370, 200, "Eiffel IUP")
  86. -- Test of polygons
  87. canvas.set_line_width(1)
  88. -- Draw an open polygon
  89. canvas.set_predefined_foreground_color(canvas.cd_dark_red)
  90. canvas.begin_open_lines
  91. canvas.vertex(50,250)
  92. canvas.vertex(80,340)
  93. canvas.vertex(150,300)
  94. canvas.vertex(110,265)
  95. canvas.vertex(90,290)
  96. canvas.end_polygon
  97. -- Draw a closed polygon
  98. canvas.set_predefined_foreground_color(canvas.cd_cyan)
  99. canvas.begin_closed_lines
  100. canvas.vertex(200,250)
  101. canvas.vertex(230,340)
  102. canvas.vertex(300,300)
  103. canvas.vertex(260,265)
  104. canvas.vertex(240,290)
  105. canvas.end_polygon
  106. -- Draw a filled polygon
  107. canvas.set_predefined_foreground_color(canvas.cd_black)
  108. canvas.set_line_style_continuous
  109. canvas.set_hatch_backward_diagonal
  110. canvas.begin_fill
  111. canvas.vertex(350,250)
  112. canvas.vertex(380,340)
  113. canvas.vertex(450,300)
  114. canvas.vertex(410,265)
  115. canvas.vertex(390,290)
  116. canvas.end_polygon
  117. -- Draw three marks
  118. canvas.set_predefined_foreground_color(canvas.cd_green)
  119. canvas.draws_mark(100, 350)
  120. canvas.draws_mark(260, 156)
  121. canvas.draws_mark(358, 289)
  122. canvas.flush
  123. end
  124. Result := "IUP_DEFAULT"
  125. end
  126. button (widget: IUP_CANVAS; btn, prs, x, y: INTEGER; status: STRING): STRING
  127. -- Draws a mark at click coordinates.
  128. local
  129. --i: INTEGER
  130. --tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER]
  131. ok: BOOLEAN
  132. canvas: CD_IUP
  133. do
  134. canvas := widget.get_cd_canvas
  135. ok := canvas.activate
  136. --i := canvas.get_font_size
  137. --tup := canvas.get_text_bounds(100, 100, "Hello")
  138. if ok then
  139. canvas.set_predefined_foreground_color(canvas.cd_green)
  140. canvas.draws_mark(x, canvas.update_y_axis(y))
  141. canvas.flush
  142. end
  143. --io.put_integer(i)
  144. --io.put_new_line
  145. --io.put_string(tup.item_1.to_string + " " + tup.item_2.to_string + " " + tup.item_3.to_string + " " + tup.item_4.to_string + " " + tup.item_5.to_string + " " + tup.item_6.to_string + " " + tup.item_7.to_string + " " + tup.item_8.to_string)
  146. --io.put_new_line
  147. Result := "IUP_DEFAULT"
  148. end
  149. rotate_canvas (widget: IUP_BUTTON): STRING
  150. do
  151. angle := (5).to_double
  152. canv.redraw
  153. Result := "IUP_DEFAULT"
  154. end
  155. end -- class CAVAS_EXAMPLE