iup_multiline.e 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. class IUP_MULTILINE
  2. -- Creates an editable field with one or more lines.
  3. inherit
  4. IUP_TEXT
  5. rename
  6. text as multiline
  7. redefine
  8. multiline
  9. end
  10. IUP_WIDGET_TEXT_POS
  11. IUP_WIDGET_TEXT_CARET
  12. export
  13. {NONE} all
  14. end
  15. IUP_WIDGET_TEXT_SELECTION
  16. export
  17. {NONE} all
  18. end
  19. IUP_WIDGET_TEXT_SPIN
  20. export
  21. {NONE} all
  22. end
  23. create {ANY}
  24. multiline
  25. feature {ANY}
  26. multiline
  27. -- A new multiline.
  28. local
  29. a_multiline, p: POINTER
  30. do
  31. a_multiline := int_multiline(p)
  32. set_widget(a_multiline)
  33. end
  34. -- Attributes
  35. set_auto_hide (state: BOOLEAN)
  36. -- Auto hide the scroll bars. Default: False. In Windows when
  37. -- FORMATTING=False, AUTOHIDE is not supported. In Motif AUTOHIDE is not
  38. -- supported.
  39. do
  40. iup_open.set_attribute(Current, "AUTOHIDE", boolean_to_yesno(state))
  41. end
  42. set_multiline_caret (lin, col: INTEGER)
  43. -- (non inheritable): Character position of the insertion point.
  44. -- The first position, lin or col, is "1". When lin is greater than the
  45. -- number of lines, the caret is placed at the last line. When col is
  46. -- greater than the number of characters in the given line, the caret is
  47. -- placed after the last character of the line.
  48. -- If the caret is not visible the text is scrolled to make it visible.
  49. -- In Windows, if the element does not have the focus the returned value
  50. -- is the position of the first character of the current selection. The
  51. -- caret is only displayed if the element has the keyboard focus, but its
  52. -- position can be changed even if not visible. When changed it will also
  53. -- change the selection.
  54. -- See the Notes above if using UTF-8 strings in GTK.
  55. require
  56. lin > 0
  57. col > 0
  58. local
  59. str: STRING
  60. do
  61. str := lin.out
  62. str.append_string(",")
  63. str.append_string(col.out)
  64. iup_open.set_attribute(Current, "CARET", str)
  65. end
  66. get_multiline_caret: TUPLE[INTEGER, INTEGER]
  67. -- The (lin, col) position of the caret.
  68. local
  69. str: STRING
  70. do
  71. str := iup_open.get_attribute(Current, "CARET")
  72. Result := components_of_position (str)
  73. end
  74. set_multiline_caret_pos (pos: INTEGER)
  75. -- (non inheritable): Also the character position of the insertion point,
  76. -- but using a zero based character unique index "pos". Useful for
  77. -- indexing the VALUE string.
  78. -- See the Notes above if using UTF-8 strings in GTK.
  79. do
  80. iup_open.set_int(Current, "CARETPOS", pos)
  81. end
  82. get_multiline_caret_pos: INTEGER
  83. do
  84. Result := iup_open.get_int(Current, "CARETPOS")
  85. end
  86. get_line_count: INTEGER
  87. -- (read-only): returns the number of lines in the text.
  88. local
  89. str: STRING
  90. do
  91. str := iup_open.get_attribute(Current, "LINECOUNT")
  92. if str.is_integer then
  93. Result := str.to_integer
  94. end
  95. end
  96. get_line_value: STRING
  97. -- (read-only): returns the text of the line where the caret is. It does
  98. -- not include the "\n" character.
  99. do
  100. Result := iup_open.get_attribute(Current, "LINEVALUE")
  101. end
  102. set_scroll_bar (value: STRING)
  103. -- Associates an automatic horizontal and/or vertical scrollbar to the
  104. -- multiline. Can be: "VERTICAL", "HORIZONTAL", "YES" (both) or "NO"
  105. -- (none). Default: "YES". For all systems, when SCROLLBAR!=NO the
  106. -- natural size will always include its size even if the native system
  107. -- hides the scrollbar. If AUTOHIDE=True scrollbars are visible only if
  108. -- they are necessary.
  109. require
  110. is_valid_scroll_bar(value)
  111. do
  112. iup_open.set_attribute(Current, "SCROLLBAR",value)
  113. end
  114. scroll_to (lin, col: INTEGER)
  115. -- (non inheritable, write only): Scroll the text to make the given
  116. -- character position visible. It uses the same format and reference of
  117. -- the CARET attribute ("lin:col" starting at 1). In Windows, when
  118. -- FORMATTING=True "col" is ignored.
  119. require
  120. lin > 0
  121. col > 0
  122. local
  123. str: STRING
  124. do
  125. str := lin.out
  126. str.append_string(",")
  127. str.append_string(col.out)
  128. iup_open.set_attribute(Current, "SCROLLTO", str)
  129. end
  130. scroll_to_pos (position: INTEGER)
  131. -- (non inheritable, write only): Scroll the text to make the given
  132. -- character position visible. It uses the same format and reference of
  133. -- the CARETPOS attribute ("pos" starting at 0).
  134. require
  135. position >= 0
  136. do
  137. iup_open.set_attribute(Current, "SCROLLTOPOS", position.out)
  138. end
  139. set_multiline_selection (lin1, col1, lin2, col2: INTEGER)
  140. -- Selection interval in characters. The first position, lin or col, is
  141. -- "1". Where lin1, col1, lin2 and col2 are integer numbers corresponding
  142. -- to the selection's interval. col2 correspond to the character after
  143. -- the last selected character.
  144. -- In Windows, when changing the selection the caret position is also
  145. -- changed.
  146. -- See the Notes above if using UTF-8 strings in GTK.
  147. local
  148. str: STRING
  149. do
  150. str := lin1.out
  151. str.append_string(",")
  152. str.append_string(col1.out)
  153. str.append_string(":")
  154. str.append_string(lin2.out)
  155. str.append_string(",")
  156. str.append_string(col2.out)
  157. iup_open.set_attribute(Current, "SELECTION", str)
  158. end
  159. get_multiline_selection: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  160. -- "lin1,col1,lin2,col2" where lin1, col1, lin2 and col2 are integer
  161. -- numbers corresponding to the selection's interval. col2 correspond to
  162. -- the character after the last selected character.
  163. local
  164. str: STRING
  165. i, c: INTEGER
  166. pos1, pos2: STRING
  167. tup1, tup2: TUPLE[INTEGER, INTEGER]
  168. tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  169. do
  170. str := iup_open.get_attribute(Current, "SELECTION")
  171. if str.has(':') then
  172. i := str.index_of(':', 1)
  173. c := str.count
  174. if not i.is_equal(1) then
  175. pos1 := str.substring(1, i - 1)
  176. else
  177. pos1 := "0,0"
  178. end
  179. if not i.is_equal(c) then
  180. pos2 := str.substring(i + 1, c)
  181. else
  182. pos2 := "0,0"
  183. end
  184. tup1 := components_of_position(pos1)
  185. tup2 := components_of_position(pos2)
  186. tup := [tup1.integer_32_item(1), tup1.integer_32_item(2),
  187. tup2.integer_32_item(1), tup2.integer_32_item(2)]
  188. else
  189. tup := [0, 0, 0, 0]
  190. end
  191. Result := tup
  192. end
  193. set_multiline_selection_pos (pos1, pos2: INTEGER)
  194. -- Same as set_multiline_selection but using a zero based character
  195. -- index. Useful for indexing the VALUE string. See the Notes above if
  196. -- using UTF-8 strings in GTK.
  197. require
  198. pos1 >= 0
  199. pos2 >= 0
  200. local
  201. str: STRING
  202. do
  203. str := pos1.out
  204. str.append_string(":")
  205. str.append_string(pos2.out)
  206. iup_open.set_attribute(Current, "SELECTIONPOS", str)
  207. end
  208. get_multiline_selection_pos: TUPLE[INTEGER, INTEGER]
  209. -- Same as get_multiline_selection but using a zero based character index.
  210. local
  211. str: STRING
  212. do
  213. str := iup_open.get_attribute(Current, "SELECTIONPOS")
  214. if str.has(':') then
  215. Result := components_of_minmax(str)
  216. else
  217. Result := [0, 0]
  218. end
  219. end
  220. set_tab_size (size: INTEGER)
  221. -- Controls the number of characters for a tab stop. Default: 8.
  222. require
  223. size >= 0
  224. do
  225. iup_open.set_attribute(Current, "TABSIZE", size.out)
  226. end
  227. get_tab_size: INTEGER
  228. -- The tab size.
  229. local
  230. str: STRING
  231. do
  232. str := iup_open.get_attribute(Current, "TABSIZE")
  233. if str.is_integer then
  234. Result := str.to_integer
  235. end
  236. end
  237. set_visible_lines (value: INTEGER)
  238. -- Defines the number of visible lines for the Natural Size, this means
  239. -- that will act also as minimum number of visible lines. As for SIZE you
  240. -- can set to Void after map to use it as an initial value. Default: 1.
  241. require
  242. value >= 0
  243. do
  244. iup_open.set_attribute(Current, "VISIBLELINES", value.out)
  245. end
  246. set_word_wrap (state: BOOLEAN)
  247. -- If enabled will force a word wrap of lines that are greater than the
  248. -- with of the control, and the horizontal scrollbar will be removed.
  249. -- Default: False.
  250. do
  251. iup_open.set_attribute(Current, "WORDWRAP", boolean_to_yesno(state))
  252. end
  253. load_rtf (filename: STRING): BOOLEAN
  254. -- (write-only) [Windows Only]: loads formatted text from a Rich
  255. -- Text Format file given its filename. Return True if file is loaded.
  256. local
  257. str: STRING
  258. do
  259. iup_open.set_attribute(Current, "LOADRTF", filename)
  260. str := iup_open.get_attribute(Current, "LOADRTFSTATUS")
  261. Result := status_to_boolean(str)
  262. end
  263. save_rtf (filename: STRING): BOOLEAN
  264. -- (write-only) [Windows Only]: saves formatted text to a Rich
  265. -- Text Format file given its filename. Return False if file is saved.
  266. local
  267. str: STRING
  268. do
  269. iup_open.set_attribute(Current, "SAVERTF", filename)
  270. str := iup_open.get_attribute(Current, "SAVERTFSTATUS")
  271. Result := status_to_boolean(str)
  272. end
  273. -- Validations
  274. is_valid_scroll_bar (value: STRING): BOOLEAN
  275. do
  276. if is_yes_no(value) or
  277. value.is_equal("HORIZONTAL") or
  278. value.is_equal("VERTICAL") then
  279. Result := True
  280. else
  281. Result := False
  282. end
  283. end
  284. feature {NONE}
  285. -- Internal
  286. int_multiline(act: POINTER): POINTER
  287. external
  288. "C inline use %"eiffel-iup.h%""
  289. alias
  290. "return IupMultiLine ($act);"
  291. end
  292. status_to_boolean(str: STRING): BOOLEAN
  293. do
  294. if str.is_equal("OK") then
  295. Result := True
  296. else
  297. Result := False
  298. end
  299. end
  300. end
  301. -- The MIT License (MIT)
  302. -- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias
  303. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  304. -- of this software and associated documentation files (the "Software"), to deal
  305. -- in the Software without restriction, including without limitation the rights
  306. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  307. -- copies of the Software, and to permit persons to whom the Software is
  308. -- furnished to do so, subject to the following conditions:
  309. --
  310. -- The above copyright notice and this permission notice shall be included in
  311. -- all copies or substantial portions of the Software.
  312. --
  313. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  314. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  315. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  316. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  317. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  318. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  319. -- SOFTWARE.