iup_formatting.e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. class IUP_FORMATTING
  2. -- The format tag element is an element with some known attributes that will be
  3. -- interpreted when the tag is updated in the native system.
  4. --
  5. -- The formatting depends on the existing text, so if VALUE attribute is set,
  6. -- all formatting is lost. You must set it again for the new text.
  7. --
  8. -- If the FONT attribute of the IupText is set then it will affect the format
  9. -- of all characters in the text.
  10. --
  11. -- The default values can not be dynamically changed.
  12. inherit
  13. IUP_USER
  14. rename
  15. user as formatting
  16. select
  17. clear_attributes
  18. end
  19. IUP_WIDGET_TEXT_SELECTION
  20. create {ANY}
  21. formatting
  22. feature {ANY}
  23. -- General Format Tag Attributes
  24. set_bulk (state: BOOLEAN)
  25. -- Flag that means this tag is composed by several tags as its children.
  26. -- Used to optimize format tag modifications. Default: False.
  27. do
  28. iup_open.set_attribute(Current, "BULK", boolean_to_yesno(state))
  29. end
  30. set_clean_out (state: BOOLEAN)
  31. -- When BULK=True is used to clear all the formatting at start.
  32. -- Default: False.
  33. do
  34. iup_open.set_attribute(Current, "CLEANOUT", boolean_to_yesno(state))
  35. end
  36. set_multiline_selection (lin1, col1, lin2, col2: INTEGER)
  37. -- Selection interval in characters. The first position, lin or col, is
  38. -- "1". Where lin1, col1, lin2 and col2 are integer numbers corresponding
  39. -- to the selection's interval. col2 correspond to the character after
  40. -- the last selected character.
  41. -- In Windows, when changing the selection the caret position is also
  42. -- changed.
  43. -- See the Notes above if using UTF-8 strings in GTK.
  44. local
  45. str: STRING
  46. do
  47. str := lin1.out
  48. str.append_string(",")
  49. str.append_string(col1.out)
  50. str.append_string(":")
  51. str.append_string(lin2.out)
  52. str.append_string(",")
  53. str.append_string(col2.out)
  54. iup_open.set_attribute(Current, "SELECTION", str)
  55. end
  56. get_multiline_selection: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  57. -- "lin1,col1,lin2,col2" where lin1, col1, lin2 and col2 are integer
  58. -- numbers corresponding to the selection's interval. col2 correspond to
  59. -- the character after the last selected character.
  60. local
  61. str: STRING
  62. i, c: INTEGER
  63. pos1, pos2: STRING
  64. tup1, tup2: TUPLE[INTEGER, INTEGER]
  65. tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
  66. do
  67. str := iup_open.get_attribute(Current, "SELECTION")
  68. if str.has(':') then
  69. i := str.index_of(':', 1)
  70. c := str.count
  71. if not i.is_equal(1) then
  72. pos1 := str.substring(1, i - 1)
  73. else
  74. pos1 := "0,0"
  75. end
  76. if not i.is_equal(c) then
  77. pos2 := str.substring(i + 1, c)
  78. else
  79. pos2 := "0,0"
  80. end
  81. tup1 := components_of_position(pos1)
  82. tup2 := components_of_position(pos2)
  83. tup := [tup1.integer_32_item(1), tup1.integer_32_item(2), tup2.integer_32_item(1), tup2.integer_32_item(2)]
  84. Result := tup
  85. else
  86. io.put_string("Unable to get the components %N")
  87. Result := [0, 0, 0, 0]
  88. end
  89. end
  90. set_units (value: STRING)
  91. -- [Windows Only]: By default all distance units are integers in pixels,
  92. -- but in Windows you can also specify integer units in TWIPs (one twip
  93. -- is 1/1440 of an inch). Can be TWIP or PIXELS. Default: PIXELS.
  94. require
  95. are_valid_units(value)
  96. do
  97. iup_open.set_attribute(Current, "UNITS", value)
  98. end
  99. -- Paragraph Format Tag Attributes
  100. set_alignment (value: STRING)
  101. -- Can be JUSTIFY, RIGHT, CENTER and LEFT. Default: LEFT.
  102. require
  103. is_valid_alignment(value)
  104. do
  105. iup_open.set_attribute(Current, "ALIGNMENT", value)
  106. end
  107. set_indent (value: INTEGER)
  108. -- Paragraph indentation, the distance between the margin and the
  109. -- paragraph. In Windows the right indentation, and the indentation of
  110. -- the second and subsequent lines (relative to the indentation of the
  111. -- first line) can be independently set using the INDENTRIGHT and
  112. -- INDENTOFFSET attributes, but only when INDENT is set.
  113. require
  114. value >= 0
  115. do
  116. iup_open.set_attribute(Current, "INDENT", value.out)
  117. end
  118. set_indent_right (value: INTEGER)
  119. -- [Windows Only] The right indentation.
  120. require
  121. value >= 0
  122. do
  123. iup_open.set_attribute(Current, "INDENTRIGHT", value.out)
  124. end
  125. set_indent_offset (value: INTEGER)
  126. -- [Windows Only] The indentation of the second and subsequent lines
  127. -- (relative to the indentation of the first line).
  128. require
  129. value >= 0
  130. do
  131. iup_open.set_attribute(Current, "INDENTOFFSET", value.out)
  132. end
  133. set_line_spacing (value: INTEGER)
  134. -- The distance between lines of the same paragraph.
  135. do
  136. iup_open.set_attribute(Current, "LINESPACING", value.out)
  137. end
  138. set_single_line_spacing
  139. -- [Windows Only] Set single distance between lines of the same
  140. -- paragraph.
  141. do
  142. iup_open.set_attribute(Current, "LINESPACING", "SINGLE")
  143. end
  144. set_onehalf_line_spacing
  145. -- [Windows Only] Set onehalf distance between lines of the same
  146. -- paragraph.
  147. do
  148. iup_open.set_attribute(Current, "LINESPACING", "ONEHALF")
  149. end
  150. set_double_line_spacing
  151. -- [Windows Only] Set double distance between lines of the same
  152. -- paragraph.
  153. do
  154. iup_open.set_attribute(Current, "LINESPACING", "DOUBLE")
  155. end
  156. set_numbering (value: STRING)
  157. -- [Windows Only]: Can be BULLET (bullet symbol), ARABIC (arabic numbers
  158. -- - 1,2,3...), LCLETTER (lowercase letters - a,b,c...), UCLETTER
  159. -- (uppercase letters - A,B,C...), LCROMAN (lowercase Roman numerals -
  160. -- i,ii,iii...), UCROMAN (uppercase Roman numerals - I,II,III...) and
  161. -- NONE. Default: NONE.
  162. require
  163. is_valid_numbering(value)
  164. do
  165. iup_open.set_attribute(Current, "NUMBERING", value)
  166. end
  167. set_numbering_style (value: STRING)
  168. -- [Windows Only]: Can be RIGHTPARENTESES "a)", PARENTESES "(a)", PERIOD
  169. -- "a.", NONUMBER (it will skip the numbering or bullet for the item) and
  170. -- NONE "". Default: NONE.
  171. require
  172. is_valid_numbering_style(value)
  173. do
  174. iup_open.set_attribute(Current, "NUMBERINGSTYLE", value)
  175. end
  176. set_numbering_tab (value: INTEGER)
  177. -- [Windows Only]: Minimum distance from a paragraph numbering or bullet
  178. -- to the paragraph text.
  179. require
  180. value >= 0
  181. do
  182. iup_open.set_attribute(Current, "NUMBERINGTAB", value.out)
  183. end
  184. set_space_after (value: INTEGER)
  185. -- Distance left empty above the paragraph.
  186. require
  187. value >= 0
  188. do
  189. iup_open.set_attribute(Current, "SPACEAFTER", value.out)
  190. end
  191. set_space_before (value: INTEGER)
  192. -- Distance left empty below the paragraph.
  193. require
  194. value >= 0
  195. do
  196. iup_open.set_attribute(Current, "SPACEBEFORE", value.out)
  197. end
  198. set_tabs_array (value: STRING)
  199. -- A sequence of tab positions and alignment up to 32 tabs. It uses the
  200. -- format:"pos align pos align pos align...". Position is the distance
  201. -- relative to the left margin and alignment can be LEFT, CENTER, RIGHT
  202. -- and DECIMAL. In GTK only LEFT is currently supported. When DECIMAL
  203. -- alignment is used, the text is aligned according to a decimal point or
  204. -- period in the text, it is normally used to align numbers.
  205. do
  206. iup_open.set_attribute(Current, "TABSARRAY", value)
  207. end
  208. -- Character Format Tag Attributes
  209. set_rgb_background_color (red: INTEGER; green: INTEGER; blue: INTEGER)
  210. -- Set the background color of the text.
  211. do
  212. iup_open.set_attribute(Current, "BGCOLOR",
  213. rgb_to_string(red, green, blue))
  214. end
  215. set_disabled (state: BOOLEAN)
  216. -- [Windows Only]: Can be YES or NO. Default NO. Set the visual
  217. -- appearance to disabled.
  218. do
  219. iup_open.set_attribute(Current, "DISABLED", boolean_to_yesno(state))
  220. end
  221. set_rgb_foreground_color (red: INTEGER; green: INTEGER; blue: INTEGER)
  222. -- Set the foreground color of the text.
  223. do
  224. iup_open.set_attribute(Current, "FGCOLOR",
  225. rgb_to_string(red, green, blue))
  226. end
  227. set_font_scale (value: REAL_32)
  228. -- A size scale relative to the selected or current size. Values greatter
  229. -- than 1 will increase the font. Values smaller than 1 will shirnk the
  230. -- font. Default: 1.0.
  231. require
  232. value >= ((0).to_real)
  233. do
  234. iup_open.set_attribute(Current, "FONTSCALE", value.out)
  235. end
  236. set_string_font_scale (value: STRING)
  237. -- The following values are accpeted: "XX-SMALL" (0.58), "X-SMALL"
  238. -- (0.64), "SMALL" (0.83), "MEDIUM" (1.0), "LARGE" (1.2), "X-LARGE"
  239. -- (1.44), "XX-LARGE" (1.73).
  240. require
  241. is_valid_font_scale(value)
  242. do
  243. iup_open.set_attribute(Current, "FONTSCALE", value)
  244. end
  245. set_fontface (fontface: STRING)
  246. -- Te face name of the font.
  247. do
  248. iup_open.set_attribute(Current, "FONTFACE", fontface)
  249. end
  250. set_fontsize (fontsize: INTEGER)
  251. -- The size of the font in pixels or points. Pixel size uses negative
  252. -- values.
  253. do
  254. iup_open.set_attribute(Current, "FONTSIZE", fontsize.out)
  255. end
  256. set_italic (state: BOOLEAN)
  257. -- Default False.
  258. do
  259. iup_open.set_attribute(Current, "ITALIC", boolean_to_yesno(state))
  260. end
  261. set_language (value: STRING)
  262. -- [GTK Only]: A text with a description of the text language. The same
  263. -- value can be used in the "SYSTEMLANGUAGE" global attribute.
  264. do
  265. iup_open.set_attribute(Current, "LANGUAGE", value)
  266. end
  267. set_rise (value: INTEGER)
  268. -- The distance, positive or negative from the base line.
  269. do
  270. iup_open.set_attribute(Current, "RISE", value.out)
  271. end
  272. set_superscript
  273. do
  274. iup_open.set_attribute(Current, "RISE", "SUPERSCRIPT")
  275. end
  276. set_subscript
  277. do
  278. iup_open.set_attribute(Current, "RISE", "SUBSCRIPT")
  279. end
  280. set_smallcaps (state: BOOLEAN)
  281. -- [GTK Only]: Default: False. (Does not work always, depends on the font)
  282. do
  283. iup_open.set_attribute(Current, "SMALLCAPS", boolean_to_yesno(state))
  284. end
  285. set_protected (state: BOOLEAN)
  286. -- Default: False. When set to True the selected text can NOT be edited.
  287. do
  288. iup_open.set_attribute(Current, "PROTECTED", boolean_to_yesno(state))
  289. end
  290. set_stretch (value: STRING)
  291. -- [GTK Only]: Can be EXTRA_CONDENSED, CONDENSED, SEMI_CONDENSED, NORMAL,
  292. -- SEMI_EXPANDED, EXPANDED and EXTRA_EXPANDED. Default NORMAL. (Does not
  293. -- work always, depends on the font)
  294. require
  295. is_valid_stretch(value)
  296. do
  297. iup_open.set_attribute(Current, "STRETCH", value)
  298. end
  299. set_strikeout (state: BOOLEAN)
  300. -- Default: False.
  301. do
  302. iup_open.set_attribute(Current, "STRIKEOUT", boolean_to_yesno(state))
  303. end
  304. set_underline (value: STRING)
  305. -- Can be SINGLE, DOUBLE, DOTTED or NONE. Default NONE. DOTTED is
  306. -- supported only in Windows.
  307. require
  308. is_valid_underline(value)
  309. do
  310. iup_open.set_attribute(Current, "UNDERLINE", value)
  311. end
  312. set_weight (value: STRING)
  313. -- Can be EXTRALIGHT, LIGHT, NORMAL, SEMIBOLD, BOLD, EXTRABOLD and
  314. -- HEAVY. Default: NORMAL.
  315. require
  316. is_valid_weight(value)
  317. do
  318. iup_open.set_attribute(Current, "WEIGHT", value)
  319. end
  320. -- Validations
  321. is_valid_alignment (value: STRING): BOOLEAN
  322. do
  323. if value.is_equal("JUSTIFY") or
  324. value.is_equal("RIGHT") or
  325. value.is_equal("CENTER") or
  326. value.is_equal("LEFT") then
  327. Result := True
  328. else
  329. Result := False
  330. end
  331. end
  332. is_valid_numbering (value: STRING): BOOLEAN
  333. do
  334. if value.is_equal("BULLET") or
  335. value.is_equal("ARABIC") or
  336. value.is_equal("LCLETTER") or
  337. value.is_equal("UCLETTER") or
  338. value.is_equal("LCROMAN") or
  339. value.is_equal("UCROMAN") or
  340. value.is_equal("NONE") then
  341. Result := True
  342. else
  343. Result := False
  344. end
  345. end
  346. is_valid_numbering_style (value: STRING): BOOLEAN
  347. do
  348. if value.is_equal("RIGHTPARENTESES") or
  349. value.is_equal("PARENTESES") or
  350. value.is_equal("PERIOD") or
  351. value.is_equal("NONUMBER") or
  352. value.is_equal("NONE") then
  353. Result := True
  354. else
  355. Result := False
  356. end
  357. end
  358. are_valid_units (value: STRING): BOOLEAN
  359. do
  360. if value.is_equal("TWIP") or
  361. value.is_equal("PIXELS") then
  362. Result := True
  363. else
  364. Result := False
  365. end
  366. end
  367. is_valid_font_scale (value: STRING): BOOLEAN
  368. do
  369. if value.is_equal("XX-SMALL") or
  370. value.is_equal("X-SMALL") or
  371. value.is_equal("SMALL") or
  372. value.is_equal("MEDIUM") or
  373. value.is_equal("LARGE") or
  374. value.is_equal("X-LARGE") or
  375. value.is_equal("XX-LARGE") then
  376. Result := True
  377. else
  378. Result := False
  379. end
  380. end
  381. is_valid_stretch (value: STRING): BOOLEAN
  382. do
  383. if value.is_equal("EXTRA_CONDENSED") or
  384. value.is_equal("CONDENSED") or
  385. value.is_equal("SEMI_CONDENSED") or
  386. value.is_equal("NORMAL") or
  387. value.is_equal("SEMI_EXPANDED") or
  388. value.is_equal("EXPANDED") or
  389. value.is_equal("EXTRA_EXPANDED") then
  390. Result := True
  391. else
  392. Result := False
  393. end
  394. end
  395. is_valid_underline (value: STRING): BOOLEAN
  396. do
  397. if value.is_equal("SINGLE") or
  398. value.is_equal("DOUBLE") or
  399. value.is_equal("DOTTED") or
  400. value.is_equal("NONE") then
  401. Result := True
  402. else
  403. Result := False
  404. end
  405. end
  406. is_valid_weight (value: STRING): BOOLEAN
  407. do
  408. if value.is_equal("EXTRALIGHT") or
  409. value.is_equal("LIGHT") or
  410. value.is_equal("NORMAL") or
  411. value.is_equal("SEMIBOLD") or
  412. value.is_equal("BOLD") or
  413. value.is_equal("EXTRABOLD") or
  414. value.is_equal("HEAVY") then
  415. Result := True
  416. else
  417. Result := False
  418. end
  419. end
  420. end
  421. -- The MIT License (MIT)
  422. -- Copyright (c) 2016, 2019 by German A. Arias
  423. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  424. -- of this software and associated documentation files (the "Software"), to deal
  425. -- in the Software without restriction, including without limitation the rights
  426. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  427. -- copies of the Software, and to permit persons to whom the Software is
  428. -- furnished to do so, subject to the following conditions:
  429. --
  430. -- The above copyright notice and this permission notice shall be included in
  431. -- all copies or substantial portions of the Software.
  432. --
  433. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  434. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  435. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  436. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  437. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  438. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  439. -- SOFTWARE.