iup_label.e 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. class IUP_LABEL
  2. -- Creates a label interface element, which displays a separator, a text or an
  3. -- image. Labels with images, texts or line separator can not change its
  4. -- behavior after mapped. But after map the image can be changed for another
  5. -- image, and the text for another text.
  6. inherit
  7. IUP_WIDGET
  8. undefine
  9. execute_dragbegin,
  10. execute_dragdatasize,
  11. execute_dragdata,
  12. execute_dragend,
  13. execute_dropdata,
  14. execute_dropmotion
  15. redefine
  16. execute_map,
  17. execute_unmap,
  18. execute_destroy,
  19. execute_enterwindow,
  20. execute_leavewindow,
  21. execute_button,
  22. execute_motion,
  23. execute_dropfiles
  24. end
  25. IUP_WIDGET_ACTIVE
  26. IUP_WIDGET_BGCOLOR
  27. IUP_WIDGET_FGCOLOR
  28. IUP_WIDGET_TITLE
  29. IUP_WIDGET_FONT
  30. IUP_WIDGET_EXPAND
  31. IUP_WIDGET_SCREENPOSITION
  32. IUP_WIDGET_POSITION
  33. IUP_WIDGET_MAXMIN_SIZE
  34. IUP_WIDGET_TIP
  35. IUP_WIDGET_SIZE
  36. IUP_WIDGET_RASTERSIZE
  37. IUP_WIDGET_USERSIZE
  38. IUP_WIDGET_ZORDER
  39. IUP_WIDGET_VISIBLE
  40. IUP_WIDGET_CHILD
  41. IUP_WIDGET_NAME
  42. IUP_DRAG_AND_DROP
  43. IUP_WIDGET_CUSTOM_ATTRIBUTES
  44. IUP_WIDGET_PADDING
  45. create {ANY}
  46. label_empty,
  47. label
  48. feature {ANY}
  49. label_empty
  50. -- A new empty label.
  51. local
  52. a_label, p: POINTER
  53. do
  54. a_label := int_label (p)
  55. set_widget(a_label)
  56. end
  57. label (text: STRING)
  58. -- A new label with the provided text
  59. local
  60. a_label: POINTER
  61. do
  62. a_label := int_label (text.to_external)
  63. set_widget(a_label)
  64. end
  65. -- Attributes
  66. set_alignment (horizontal, vertical: STRING)
  67. -- (non inheritable): horizontal and vertical alignment. Possible values:
  68. -- "ALEFT", "ACENTER" and "ARIGHT", combined to "ATOP", "ACENTER" and
  69. -- "ABOTTOM". Default: "ALEFT:ACENTER". In Motif, vertical alignment is
  70. -- restricted to "ACENTER".
  71. require
  72. is_valid_alignment(horizontal, vertical)
  73. local
  74. str: STRING
  75. do
  76. create str.copy(horizontal)
  77. str.append_string(":")
  78. str.append_string(vertical)
  79. iup_open.set_attribute(Current, "ALIGNMENT", str)
  80. end
  81. set_as_drop_files_target (state: BOOLEAN)
  82. -- [Windows and GTK Only] (non inheritable): Enable or disable the drop
  83. -- of files. Default: False, but if DROPFILES_CB is defined when the
  84. -- element is mapped then it will be automatically enabled.
  85. do
  86. iup_open.set_attribute(Current, "DROPFILESTARGET", boolean_to_yesno(state))
  87. end
  88. set_ellipsis (state: BOOLEAN)
  89. -- [Windows and GTK only]: add an ellipsis: "..." to the text if there
  90. -- is not enough space to render the entire string. Can be "YES" or "NO".
  91. -- Default: "False".
  92. do
  93. iup_open.set_attribute(Current, "ELLIPSIS", boolean_to_yesno(state))
  94. end
  95. set_image (name: STRING)
  96. -- (non inheritable): Image name. If set before map defines the behavior
  97. -- of the button to contain an image. The natural size will be size of
  98. -- the image in pixels. Use set_attribute_handle to associate an image to
  99. -- a name. See also IUP_IMAGE.
  100. do
  101. iup_open.set_attribute(Current, "IMAGE", name)
  102. end
  103. set_image_inactive (name: STRING)
  104. -- (non inheritable) [GTK and Motif only]: Image name of the element when
  105. -- inactive. If it is not defined then the IMAGE is used and the colors
  106. -- will be replaced by a modified version of the background color
  107. -- creating the disabled effect. GTK will also change the inactive image
  108. -- to look like other inactive objects.
  109. do
  110. iup_open.set_attribute(Current, "IMINACTIVE", name)
  111. end
  112. set_markup (state: BOOLEAN)
  113. -- [GTK only]: allows the title string to contains pango markup
  114. -- commands. Works only if a mnemonic is NOT defined in the title. Can
  115. -- be "True" or "False". Default: "False".
  116. do
  117. iup_open.set_attribute(Current, "MARKUP", boolean_to_yesno(state))
  118. end
  119. set_horizontal_separator
  120. -- (creation only) (non inheritable): Turns the label into a horizontal
  121. -- line separator. When changed before mapping the EXPAND attribute is
  122. -- set to "HORIZONTALFREE". (Since 3.11 changed to FREE based expand)
  123. do
  124. iup_open.set_attribute(Current, "SEPARATOR", "HORIZONTAL")
  125. end
  126. set_vertical_separator
  127. -- (creation only) (non inheritable): Turns the label into a vertical line
  128. -- separator. When changed before mapping the EXPAND attribute is set to
  129. -- "VERTICALFREE" accordingly. (Since 3.11 changed to FREE based expand)
  130. do
  131. iup_open.set_attribute(Current, "SEPARATOR", "VERTICAL")
  132. end
  133. set_word_wrap (state: BOOLEAN)
  134. -- [Windows and GTK only]: enables or disable the wrapping of lines that
  135. -- does not fits in the label. Can be "YES" or "NO". Default: "NO". Can
  136. -- only set WORDWRAP=YES if ALIGNMENT=ALEFT.
  137. do
  138. iup_open.set_attribute(Current, "WORDWRAP", boolean_to_yesno(state))
  139. end
  140. -- Callbacks
  141. -- Common
  142. set_cb_map (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
  143. -- Called right after an element is mapped and its attributes updated.
  144. local
  145. operation: INTEGER
  146. do
  147. cb_map := act
  148. if cb_map /= Void then
  149. operation := 1
  150. else
  151. operation := 0
  152. end
  153. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  154. end
  155. set_cb_unmap (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
  156. -- Called right before an element is unmapped.
  157. local
  158. operation: INTEGER
  159. do
  160. cb_unmap := act
  161. if cb_unmap /= Void then
  162. operation := 1
  163. else
  164. operation := 0
  165. end
  166. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  167. end
  168. set_cb_destroy (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
  169. -- Called right before an element is destroyed.
  170. local
  171. operation: INTEGER
  172. do
  173. cb_destroy := act
  174. if cb_destroy /= Void then
  175. operation := 1
  176. else
  177. operation := 0
  178. end
  179. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  180. end
  181. set_cb_enter_window (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
  182. -- Action generated when the mouse enters the native element.
  183. local
  184. operation: INTEGER
  185. do
  186. cb_enterwindow := act
  187. if cb_enterwindow /= Void then
  188. operation := 1
  189. else
  190. operation := 0
  191. end
  192. iup_open.set_callback (Current, "ENTERWINDOW_CB", "NONEEDED", operation)
  193. end
  194. set_cb_leave_window (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
  195. -- Action generated when the mouse leaves the native element.
  196. local
  197. operation: INTEGER
  198. do
  199. cb_leavewindow := act
  200. if cb_leavewindow /= Void then
  201. operation := 1
  202. else
  203. operation := 0
  204. end
  205. iup_open.set_callback (Current, "LEAVEWINDOW_CB", "NONEEDED", operation)
  206. end
  207. -- Extra
  208. set_cb_button (act: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING])
  209. -- Action generated when any mouse button is pressed and when it is
  210. -- released. Both calls occur before the ACTION callback when button 1 is
  211. -- being used.
  212. --
  213. -- IUP_BUTTON: identifies the element that activated the event.
  214. -- button: identifies the activated mouse button:
  215. --
  216. -- 1 - left mouse button (button 1);
  217. -- 2 - middle mouse button (button 2);
  218. -- 3 - right mouse button (button 3).
  219. --
  220. -- pressed: indicates the state of the button:
  221. --
  222. -- 0 - mouse button was released;
  223. -- 1 - mouse button was pressed.
  224. --
  225. -- x, y: position in the canvas where the event has occurred, in pixels.
  226. --
  227. -- status: status of the mouse buttons and some keyboard keys at the
  228. -- moment the event is generated. The following IUP features must be used
  229. -- for verification:
  230. --
  231. -- is_shift(status)
  232. -- is_control(status)
  233. -- is_button_1(status)
  234. -- is_button_2(status)
  235. -- is_button_3(status)
  236. -- is_button_4(status)
  237. -- is_button_5(status)
  238. -- is_double(status)
  239. -- is_alt(status)
  240. -- is_sys(status)
  241. --
  242. -- Returns: IUP_CLOSE will be processed. On some controls if IUP_IGNORE
  243. -- is returned the action is ignored (this is system dependent).
  244. local
  245. operation: INTEGER
  246. do
  247. cb_button := act
  248. if cb_button /= Void then
  249. operation := 1
  250. else
  251. operation := 0
  252. end
  253. iup_open.set_callback (Current, "BUTTON_CB", "NONEEDED", operation)
  254. end
  255. set_cb_motion (act: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, STRING], STRING])
  256. -- Action generated when the mouse moves.
  257. -- ih: identifier of the element that activated the event.
  258. -- x, y: position in the canvas where the event has occurred, in pixels.
  259. -- status: status of mouse buttons and certain keyboard keys at the
  260. -- moment the event was generated. The same macros used for BUTTON_CB can
  261. -- be used for this status.
  262. local
  263. operation: INTEGER
  264. do
  265. cb_motion := act
  266. if cb_motion /= Void then
  267. operation := 1
  268. else
  269. operation := 0
  270. end
  271. iup_open.set_callback (Current, "MOTION_CB", "NONEEDED", operation)
  272. end
  273. set_cb_drop_files (act: FUNCTION[TUPLE[IUP_LABEL, STRING, INTEGER, INTEGER, INTEGER], STRING])
  274. local
  275. operation: INTEGER
  276. do
  277. cb_dropfiles := act
  278. if cb_dropfiles /= Void then
  279. operation := 1
  280. else
  281. operation := 0
  282. end
  283. iup_open.set_callback (Current, "DROPFILES_CB", "NONEEDED", operation)
  284. end
  285. feature {IUP}
  286. execute_map: STRING
  287. do
  288. Result := cb_map.item([Current])
  289. end
  290. execute_unmap: STRING
  291. do
  292. Result := cb_unmap.item([Current])
  293. end
  294. execute_destroy: STRING
  295. do
  296. Result := cb_destroy.item([Current])
  297. end
  298. execute_enterwindow: STRING
  299. do
  300. Result := cb_enterwindow.item([Current])
  301. end
  302. execute_leavewindow: STRING
  303. do
  304. Result := cb_leavewindow.item([Current])
  305. end
  306. execute_button (btn, pressed, x, y: INTEGER; status: STRING): STRING
  307. do
  308. Result := cb_button.item([Current, btn, pressed, x, y, status])
  309. end
  310. execute_motion (x, y: INTEGER; status: STRING): STRING
  311. do
  312. Result := cb_motion.item([Current, x, y, status])
  313. end
  314. execute_dropfiles (filename: STRING; num: INTEGER; x: INTEGER; y: INTEGER): STRING
  315. do
  316. Result := cb_dropfiles.item([Current, filename, num, x, y])
  317. end
  318. feature {}
  319. -- For callbacks
  320. cb_map: FUNCTION[TUPLE[IUP_LABEL], STRING]
  321. cb_unmap: FUNCTION[TUPLE[IUP_LABEL], STRING]
  322. cb_destroy: FUNCTION[TUPLE[IUP_LABEL], STRING]
  323. cb_enterwindow: FUNCTION[TUPLE[IUP_LABEL], STRING]
  324. cb_leavewindow: FUNCTION[TUPLE[IUP_LABEL], STRING]
  325. cb_button: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING]
  326. cb_motion: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, STRING], STRING]
  327. cb_dropfiles: FUNCTION[TUPLE[IUP_LABEL, STRING, INTEGER, INTEGER, INTEGER], STRING]
  328. -- Internals
  329. int_label (text: POINTER): POINTER
  330. external "plug_in"
  331. alias "{
  332. location: "${sys}/plugins"
  333. module_name: "iup"
  334. feature_name: "IupLabel"
  335. }"
  336. end
  337. -- Validations
  338. is_valid_alignment (horizontal, vertical: STRING): BOOLEAN
  339. local
  340. h, v: BOOLEAN
  341. do
  342. if horizontal.is_equal("ALEFT") or
  343. horizontal.is_equal("ACENTER") or
  344. horizontal.is_equal("ARIGHT") then
  345. h := True
  346. else
  347. h := False
  348. end
  349. if vertical.is_equal("ATOP") or
  350. vertical.is_equal("ACENTER") or
  351. vertical.is_equal("ABOTTOM") then
  352. v := True
  353. else
  354. v := False
  355. end
  356. if h and v then
  357. Result := True
  358. else
  359. Result := False
  360. end
  361. end
  362. end -- class IUP_LABEL
  363. -- The MIT License (MIT)
  364. -- Copyright (c) 2016, 2017, 2018, 2019 by German A. Arias
  365. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  366. -- of this software and associated documentation files (the "Software"), to deal
  367. -- in the Software without restriction, including without limitation the rights
  368. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  369. -- copies of the Software, and to permit persons to whom the Software is
  370. -- furnished to do so, subject to the following conditions:
  371. --
  372. -- The above copyright notice and this permission notice shall be included in
  373. -- all copies or substantial portions of the Software.
  374. --
  375. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  376. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  377. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  378. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  379. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  380. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  381. -- SOFTWARE.