iup_get_color.e 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. class IUP_GET_COLOR
  2. -- Shows a modal dialog which allows the user to select a color. Based on
  3. -- IUP_COLOR_DIALOG.
  4. --
  5. -- The dialog uses a global attribute called "PARENTDIALOG" as the parent
  6. -- dialog if it is defined. It also uses a global attribute called "ICON" as
  7. -- the dialog icon if it is defined.
  8. inherit
  9. IUP_WIDGET
  10. create {ANY}
  11. get_color_with_initial
  12. feature {ANY}
  13. get_color_with_initial (red, green, blue: INTEGER)
  14. -- Create a color dialog with the specified color selected when
  15. -- the dialog is shown.
  16. require
  17. red >= 0
  18. red <= 255
  19. green >= 0
  20. green <= 255
  21. blue >= 0
  22. blue <= 255
  23. do
  24. r := red
  25. g := green
  26. b := blue
  27. end
  28. launch_predefined_xy (x, y: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  29. -- Show the dialog using predefined values for its position. However
  30. -- you can combine a predefined value with an integer (as string).
  31. -- Return a tuple with four integer values. The first is 1 if the OK
  32. -- button is pressed, 0 otherwise. The rest are the RGB values of the
  33. -- selected color.
  34. --
  35. -- x: Predefined horizontal position. The following definitions
  36. -- can be used:
  37. --
  38. -- IUP_LEFT: Positions the element on the left corner of the main screen.
  39. -- IUP_CENTER: Centers the element on the main screen.
  40. -- IUP_RIGHT: Positions the element on the right corner of the main screen
  41. -- IUP_MOUSEPOS: Positions the element on the mouse cursor
  42. -- IUP_CENTERPARENT: Horizontally centralizes the dialog relative to its
  43. -- parent. (Since 3.0)
  44. -- IUP_CURRENT: use the current position of the dialog.. (Since 3.0)
  45. --
  46. -- y: Predefined vertical position. The following definitions
  47. -- can be used:
  48. --
  49. -- IUP_TOP: Positions the element on the top of the main screen
  50. -- IUP_CENTER: Vertically centers the element on the main screen
  51. -- IUP_BOTTOM: Positions the element on the base of the main screen
  52. -- IUP_MOUSEPOS: Positions the element on the mouse cursor
  53. -- IUP_CENTERPARENT: Vertically centralizes the dialog relative to its
  54. -- parent. (Since 3.0)
  55. -- IUP_CURRENT: use the current position of the dialog. (Since 3.0)
  56. require
  57. is_valid_position(x, y)
  58. do
  59. Result := launch(iup_open.position_to_integer(x),
  60. iup_open.position_to_integer(y))
  61. end
  62. launch (x, y: INTEGER): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  63. -- Show the dialog. Return a tuple with four integer values. The first
  64. -- is 1 if the OK button is pressed, 0 otherwise. The rest are the
  65. -- RGB values of the selected color.
  66. --
  67. -- x: horizontal position of the top left corner of the window,
  68. -- relative to the origin of the main screen.
  69. --
  70. -- y: vertical position of the top left corner of the window or menu,
  71. -- relative to the origin of the main screen.
  72. local
  73. sr, sg, sb, otr, otg, otb: STRING
  74. pr, pg, pb: POINTER
  75. i: INTEGER
  76. tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  77. do
  78. create sr.make(10)
  79. create sg.make(10)
  80. create sb.make(10)
  81. sr.append_string(r.out)
  82. sg.append_string(g.out)
  83. sb.append_string(b.out)
  84. pr := get_pointer(sr.to_c)
  85. pg := get_pointer(sg.to_c)
  86. pb := get_pointer(sb.to_c)
  87. i := int_get_color (x, y, pr, pg, pb)
  88. create otr.make_from_c(pr)
  89. create otg.make_from_c(pg)
  90. create otb.make_from_c(pb)
  91. if otr.is_empty then
  92. otr.append_string("0")
  93. end
  94. if otg.is_empty then
  95. otg.append_string("0")
  96. end
  97. if otb.is_empty then
  98. otb.append_string("0")
  99. end
  100. tup := [i, otr.item(1).code,
  101. otg.item(1).code,
  102. otb.item(1).code]
  103. Result := tup
  104. end
  105. -- Validations
  106. is_valid_position (x, y: STRING): BOOLEAN
  107. local
  108. xs, ys: BOOLEAN
  109. do
  110. if x.is_equal("IUP_LEFT") or
  111. x.is_equal("IUP_CENTER") or
  112. x.is_equal("IUP_RIGHT") or
  113. x.is_equal("IUP_MOUSEPOS") or
  114. x.is_equal("IUP_CENTERPARENT") or
  115. x.is_equal("IUP_CURRENT") then
  116. xs := True
  117. elseif x.is_integer and x.to_integer >= 0 then
  118. xs := True
  119. else
  120. xs := False
  121. end
  122. if y.is_equal("IUP_TOP") or
  123. y.is_equal("IUP_CENTER") or
  124. y.is_equal("IUP_BOTTOM") or
  125. y.is_equal("IUP_MOUSEPOS") or
  126. y.is_equal("IUP_CENTERPARENT") or
  127. y.is_equal("IUP_CURRENT") then
  128. ys := True
  129. elseif y.is_integer and y.to_integer >= 0 then
  130. ys := True
  131. else
  132. ys := False
  133. end
  134. if xs and ys then
  135. Result := True
  136. else
  137. Result := False
  138. end
  139. end
  140. feature {NONE}
  141. r, g, b: INTEGER
  142. -- Internal
  143. int_get_color (x, y: INTEGER; rv, gv, bv: POINTER): INTEGER
  144. external
  145. "C inline use %"eiffel-iup.h%""
  146. alias
  147. "return IupGetColor ($x, $y, $rv, $gv, $bv);"
  148. end
  149. end -- end IUP_GET_COLOR
  150. -- The MIT License (MIT)
  151. -- Copyright (c) 2016, 2017, 2019 by German A. Arias
  152. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  153. -- of this software and associated documentation files (the "Software"), to deal
  154. -- in the Software without restriction, including without limitation the rights
  155. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  156. -- copies of the Software, and to permit persons to whom the Software is
  157. -- furnished to do so, subject to the following conditions:
  158. --
  159. -- The above copyright notice and this permission notice shall be included in
  160. -- all copies or substantial portions of the Software.
  161. --
  162. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  163. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  164. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  165. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  166. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  167. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  168. -- SOFTWARE.