/tutorial/vision/widget/colors.e

http://github.com/tybor/Liberty · Specman e · 179 lines · 146 code · 17 blank · 16 comment · 3 complexity · 417aa21c2393947230c190ad7d14a26f MD5 · raw file

  1. class COLORS
  2. -- This example show COLORS from the list and produce color_list.e file
  3. inherit
  4. GRAPHIC
  5. insert
  6. ARGUMENTS
  7. creation {ANY}
  8. make
  9. feature {}
  10. scroll: SCROLL_VIEW
  11. make is
  12. local
  13. main_window: TOPLEVEL_WINDOW; color_list: SUB_WINDOW; line: HORIZONTAL_LINE
  14. do
  15. if argument_count /= 1 then
  16. io.put_string("Give the color list file name as parameter.%N")
  17. io.put_string("You may use SmartEiffel/lib/vision/color/small_color_list.txt%N")
  18. else
  19. create main_window
  20. -- The default layout is column.
  21. create scroll.make(main_window)
  22. scroll.set_x_expand(True)
  23. scroll.set_y_expand(True)
  24. scroll.map
  25. create color_list.make_layout(scroll, create {COLUMN_LAYOUT})
  26. color_list.set_x_expand(True)
  27. color_list.set_y_expand(True)
  28. color_list.set_background_color(white_color)
  29. color_list.layout_pause
  30. add_colors(color_list, argument(1))
  31. color_list.layout_continue
  32. color_list.map
  33. color_list.when_left_down(agent set_move(True))
  34. color_list.when_left_up(agent set_move(False))
  35. color_list.when_pointer_move(agent moving)
  36. create line
  37. main_window.child_attach(line)
  38. add_controls(main_window)
  39. main_window.set_title("Scroll example")
  40. main_window.set_background_color(white_color)
  41. main_window.map
  42. vision.start
  43. end
  44. end
  45. add_controls (w: WINDOW) is
  46. local
  47. controls: CONTAINER; layout: ROW_LAYOUT; quit: BUTTON; shift: BUTTON
  48. do
  49. create layout
  50. create controls.make_layout(w, layout)
  51. controls.set_x_expand(True)
  52. layout.set_border(5)
  53. layout.set_spacing(5)
  54. layout.insert_button_space
  55. create shift.with_label(controls, U"up")
  56. shift.when_left_clicked(agent scroll.vertical_shift(-50))
  57. create shift.with_label(controls, U"down")
  58. shift.when_left_clicked(agent scroll.vertical_shift(50))
  59. create shift.with_label(controls, U"left")
  60. shift.when_left_clicked(agent scroll.horizontal_shift(-20))
  61. create shift.with_label(controls, U"right")
  62. shift.when_left_clicked(agent scroll.horizontal_shift(20))
  63. layout.insert_button_space
  64. create quit.with_label(controls, U"Exit")
  65. quit.when_left_clicked(agent vision.loop_stack.break)
  66. end
  67. move: BOOLEAN
  68. click_x, click_y: INTEGER
  69. set_move (b: BOOLEAN) is
  70. do
  71. move := b
  72. click_x := vision.pointer_x_root
  73. click_y := vision.pointer_y_root
  74. end
  75. moving (x, y: INTEGER) is
  76. do
  77. if move then
  78. scroll.shift(vision.pointer_x_root - click_x, vision.pointer_y_root - click_y)
  79. click_x := vision.pointer_x_root
  80. click_y := vision.pointer_y_root
  81. end
  82. end
  83. add_colors (c: CONTAINER; file_name: STRING) is
  84. local
  85. txt: TEXT_FILE_READ; class_file: TEXT_FILE_WRITE; r, g, b: INTEGER
  86. do
  87. create txt.connect_to(file_name)
  88. if txt.is_connected then
  89. create class_file.connect_to("new_color_list.e")
  90. class_file.put_string(header)
  91. from
  92. txt.read_integer
  93. r := txt.last_integer
  94. until
  95. txt.end_of_input
  96. loop
  97. txt.read_integer
  98. g := txt.last_integer
  99. txt.read_integer
  100. b := txt.last_integer
  101. txt.skip_separators
  102. txt.read_line
  103. show_color(c, r, g, b, txt.last_string)
  104. txt.last_string.to_lower
  105. txt.last_string.replace_all(' ', '_')
  106. txt.last_string.append(once "_color")
  107. write(class_file, r, g, b, txt.last_string)
  108. txt.read_integer
  109. r := txt.last_integer
  110. end
  111. class_file.put_string("end%N")
  112. class_file.disconnect
  113. txt.disconnect
  114. end
  115. end
  116. write (output: OUTPUT_STREAM; r, g, b: INTEGER; name: STRING) is
  117. do
  118. output.put_string(once " ")
  119. output.put_string(name)
  120. output.put_string(once ": COLOR is%N once%N create Result.like_rgb_8(")
  121. output.put_integer(r)
  122. output.put_character(',')
  123. output.put_integer(g)
  124. output.put_character(',')
  125. output.put_integer(b)
  126. output.put_string(once ")%N end%N%N")
  127. end
  128. show_color (c: CONTAINER; r, g, b: INTEGER; name: STRING) is
  129. local
  130. label: LABEL; color: COLOR; item: CONTAINER; rect: FILL_RECTANGLE
  131. do
  132. create item.make_layout(c, create {ROW_LAYOUT})
  133. create color.like_rgb_8(r, g, b)
  134. create rect.make(10, 10, 100, 30)
  135. rect.set_style(create {DRAW_STYLE})
  136. rect.style.set_color(color)
  137. item.set_x_expand(True)
  138. item.child_attach(rect)
  139. create label.make(create {UNICODE_STRING}.from_utf8(name))
  140. item.child_attach(label)
  141. end
  142. header: STRING is "[
  143. -- This file is free software, which comes along with SmartEiffel. This
  144. -- software is distributed in the hope that it will be useful, but WITHOUT
  145. -- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  146. -- FITNESS FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  147. -- this header is kept unaltered, and a notification of the changes is added.
  148. -- You are allowed to redistribute it and sell it, alone or as a part of
  149. -- another product.
  150. -- Copyright (C) 1994-2002 LORIA - INRIA - U.H.P. Nancy 1 - FRANCE
  151. -- Philippe RIBET and Dominique COLNET - SmartEiffel@loria.fr
  152. -- http://SmartEiffel.loria.fr
  153. --
  154. class COLOR_LIST
  155. --
  156. -- THIS FILE WAS GENERATED. DON'T EDIT
  157. --
  158. insert
  159. ANY
  160. feature {}
  161. ]"
  162. end -- class COLORS