iup_date_pick.e 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. class IUP_DATE_PICK
  2. -- Creates a date editing interface element, which can displays a calendar for
  3. -- selecting a date.
  4. --
  5. -- In Windows is a native element. In GTK and Motif is a custom element. In
  6. -- Motif is not capable of displaying the calendar.
  7. inherit
  8. IUP_WIDGET
  9. redefine
  10. execute_map,
  11. execute_unmap,
  12. execute_destroy,
  13. execute_getfocus,
  14. execute_killfocus,
  15. execute_enterwindow,
  16. execute_leavewindow,
  17. execute_k_any,
  18. execute_help,
  19. execute_valuechanged
  20. end
  21. IUP_WIDGET_INTERNALS
  22. IUP_WIDGET_CHILD
  23. IUP_WIDGET_NAME
  24. IUP_WIDGET_CUSTOM_ATTRIBUTES
  25. create {ANY}
  26. date_pick
  27. feature {ANY}
  28. date_pick
  29. -- Creates a month calendar interface element.
  30. local
  31. a_date_pick: POINTER
  32. do
  33. a_date_pick := int_date_pick
  34. set_widget(a_date_pick)
  35. end
  36. -- Attributes
  37. set_calendar_week_number (state: BOOLEAN)
  38. -- Shows the number of the week along the year. Default: False.
  39. do
  40. iup_open.set_attribute(Current, "CALENDARWEEKNUMBERS",
  41. boolean_to_yesno(state))
  42. end
  43. set_format (value: STRING)
  44. -- [Windows Only]: Flexible format for the date in Windows. For more
  45. -- information see "About Date and Time Picker Control" in the Windows
  46. -- SDK: https://msdn.microsoft.com/EN-US/library/windows/desktop/bb761726(v=vs.85).aspx
  47. -- The Windows control was configured to display date only without any
  48. -- time options. Default: "d'/'M'/'yyyy".
  49. --
  50. -- In Windows, when the user navigates to other pages in the calendar the
  51. -- date is not changed until the user actually selects a day.
  52. --
  53. -- In Windows, FORMAT can have the following values, but other text in
  54. -- the format string must be enclosed in single quotes:
  55. --
  56. -- Element Description
  57. -- "d" The one- or two-digit day. (default)
  58. -- "dd" The two-digit day. Single-digit day values are
  59. -- preceded by a zero.
  60. -- (Set when ZEROPRECED=True)
  61. -- "ddd" The three-character weekday abbreviation.
  62. -- "dddd" The full weekday name.
  63. -- "M" The one- or two-digit month number. (default)
  64. -- "MM" The two-digit month number. Single-digit values are
  65. -- preceded by a zero.
  66. -- (Set when ZEROPRECED=True)
  67. -- "MMM" The three-character month abbreviation.
  68. -- (Set when MONTHSHORTNAMES=True)
  69. -- "MMMM" The full month name.
  70. -- "yy" The last two digits of the year (that is, 1996 would be
  71. -- displayed as "96"). (Not recommended)
  72. -- "yyyy" The full year (that is, 1996 would be displayed as
  73. -- "1996"). (default)
  74. do
  75. iup_open.set_attribute(Current, "FORMAT", value)
  76. end
  77. set_month_short_names (state: BOOLEAN)
  78. -- [Windows Only]: Month display will use a short name instead of
  79. -- numbers. Must be set before ORDER. Default: False. Names will be in
  80. -- the language of the system.
  81. do
  82. iup_open.set_attribute(Current, "MONTHSHORTNAMES",
  83. boolean_to_yesno(state))
  84. end
  85. set_order (value: STRING)
  86. -- Day, month and year order. Can be any combination of "D", "M" and "Y"
  87. -- without repetition, and with all three letters. It will set the FORMAT
  88. -- attribute in Windows. It will NOT affect the VALUE attribute order.
  89. -- Default: "DMY".
  90. require
  91. is_valid_order(value)
  92. do
  93. iup_open.set_attribute(Current, "ORDER", value)
  94. end
  95. set_separator (value: STRING)
  96. -- Separator between day, month and year. Must be set before ORDER in
  97. -- Windows. Default: "/".
  98. do
  99. iup_open.set_attribute(Current, "SEPARATOR", value)
  100. end
  101. get_today: TUPLE[INTEGER, INTEGER, INTEGER]
  102. -- (read-only): Returns the date corresponding to today in format
  103. -- [year, month, day]".
  104. do
  105. Result := convert_date_to_tuple(get_string_today)
  106. end
  107. get_string_today: STRING
  108. -- (read-only): Returns the date corresponding to today in format
  109. -- "year/month/day".
  110. do
  111. Result := iup_open.get_attribute(Current, "TODAY")
  112. end
  113. set_value (year, month, day: INTEGER)
  114. -- The current date
  115. require
  116. year >= 0
  117. month >= 1
  118. month <= 12
  119. day >= 1
  120. day <= 31
  121. local
  122. str: STRING
  123. do
  124. str := year.to_string
  125. str.append_string("/")
  126. str.append_string(month.to_string)
  127. str.append_string("/")
  128. str.append_string(day.to_string)
  129. iup_open.set_attribute(Current, "VALUE", str)
  130. end
  131. set_string_value (value: STRING)
  132. -- The current date always in the format "year/month/day"
  133. require
  134. valid_date: is_valid_string_date(value)
  135. do
  136. iup_open.set_attribute(Current, "VALUE", value)
  137. end
  138. set_today
  139. -- Set value to today date.
  140. do
  141. iup_open.set_attribute(Current, "VALUE", "TODAY")
  142. end
  143. get_value: TUPLE[INTEGER, INTEGER, INTEGER]
  144. -- Return the selected date in format [year, month, day]".
  145. do
  146. Result := convert_date_to_tuple(get_string_value)
  147. end
  148. get_string_value: STRING
  149. -- Return the selected date in format "year/month/day".
  150. do
  151. Result := iup_open.get_attribute(Current, "VALUE")
  152. end
  153. set_zero_preced (state: BOOLEAN)
  154. -- Day and month numbers will be preceded by a zero. Must be set before
  155. -- ORDER in Windows. Default: False.
  156. do
  157. iup_open.set_attribute(Current, "ZEROPRECED",
  158. boolean_to_yesno(state))
  159. end
  160. -- Callbacks
  161. -- Common
  162. set_cb_map (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  163. -- Called right after an element is mapped and its attributes updated.
  164. local
  165. operation: INTEGER
  166. do
  167. cb_map := act
  168. if cb_map /= Void then
  169. operation := 1
  170. else
  171. operation := 0
  172. end
  173. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  174. end
  175. set_cb_unmap (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  176. -- Called right before an element is unmapped.
  177. local
  178. operation: INTEGER
  179. do
  180. cb_unmap := act
  181. if cb_unmap /= Void then
  182. operation := 1
  183. else
  184. operation := 0
  185. end
  186. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  187. end
  188. set_cb_destroy (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  189. -- Called right before an element is destroyed.
  190. local
  191. operation: INTEGER
  192. do
  193. cb_destroy := act
  194. if cb_destroy /= Void then
  195. operation := 1
  196. else
  197. operation := 0
  198. end
  199. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  200. end
  201. set_cb_get_focus (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  202. -- Action generated when an element is given keyboard focus.
  203. -- This callback is called after the KILLFOCUS_CB of the element
  204. -- that loosed the focus. The {IUP}.get_focus function during the
  205. -- callback returns the element that loosed the focus.
  206. local
  207. operation: INTEGER
  208. do
  209. cb_getfocus := act
  210. if cb_getfocus /= Void then
  211. operation := 1
  212. else
  213. operation := 0
  214. end
  215. iup_open.set_callback (Current, "GETFOCUS_CB", "NONEEDED", operation)
  216. end
  217. set_cb_kill_focus (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  218. -- Action generated when an element loses keyboard focus. This
  219. -- callback is called before the GETFOCUS_CB of the element that
  220. -- gets the focus.
  221. local
  222. operation: INTEGER
  223. do
  224. cb_killfocus := act
  225. if cb_killfocus /= Void then
  226. operation := 1
  227. else
  228. operation := 0
  229. end
  230. iup_open.set_callback (Current, "KILLFOCUS_CB", "NONEEDED", operation)
  231. end
  232. set_cb_enter_window (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  233. -- Action generated when the mouse enters the native element.
  234. local
  235. operation: INTEGER
  236. do
  237. cb_enterwindow := act
  238. if cb_enterwindow /= Void then
  239. operation := 1
  240. else
  241. operation := 0
  242. end
  243. iup_open.set_callback (Current, "ENTERWINDOW_CB", "NONEEDED", operation)
  244. end
  245. set_cb_leave_window (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  246. -- Action generated when the mouse leaves the native element.
  247. local
  248. operation: INTEGER
  249. do
  250. cb_leavewindow := act
  251. if cb_leavewindow /= Void then
  252. operation := 1
  253. else
  254. operation := 0
  255. end
  256. iup_open.set_callback (Current, "LEAVEWINDOW_CB", "NONEEDED", operation)
  257. end
  258. set_cb_k_any (act: FUNCTION[TUPLE[IUP_DATE_PICK, INTEGER], STRING])
  259. -- Action generated when a keyboard event occurs.
  260. -- IUP_WIDGET the element that activated the event.
  261. -- INTEGER identifier of typed key. Please refer to the Keyboard
  262. -- Codes table for a list of possible values.
  263. --
  264. -- Returns: If IUP_IGNORE is returned the key is ignored and not
  265. -- processed by the control and not propagated. If returns
  266. -- IUP_CONTINUE, the key will be processed and the event will be
  267. -- propagated to the parent of the element receiving it, this is
  268. -- the default behavior. If returns IUP_DEFAULT the key is processed
  269. -- but it is not propagated. IUP_CLOSE will be processed.
  270. local
  271. operation: INTEGER
  272. do
  273. cb_k_any := act
  274. if cb_k_any /= Void then
  275. operation := 1
  276. else
  277. operation := 0
  278. end
  279. iup_open.set_callback (Current, "K_ANY", "NONEEDED", operation)
  280. end
  281. set_cb_help (act: PROCEDURE[TUPLE[IUP_DATE_PICK]])
  282. -- Action generated when the user press F1 at a control. In Motif
  283. -- is also activated by the Help button in some workstations
  284. -- keyboard.
  285. -- Returns: IUP_CLOSE will be processed.
  286. local
  287. operation: INTEGER
  288. do
  289. cb_help := act
  290. if cb_help /= Void then
  291. operation := 1
  292. else
  293. operation := 0
  294. end
  295. iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
  296. end
  297. -- Extra
  298. set_cb_value_changed (act: FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  299. local
  300. operation: INTEGER
  301. do
  302. cb_valuechanged := act
  303. if cb_valuechanged /= Void then
  304. operation := 1
  305. else
  306. operation := 0
  307. end
  308. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  309. end
  310. feature {IUP} -- Internal handle of callbacks
  311. -- Common callbacks
  312. execute_map: STRING
  313. do
  314. Result := cb_map.item([Current])
  315. end
  316. execute_unmap: STRING
  317. do
  318. Result := cb_unmap.item([Current])
  319. end
  320. execute_destroy: STRING
  321. do
  322. Result := cb_destroy.item([Current])
  323. end
  324. execute_getfocus: STRING
  325. do
  326. Result := cb_getfocus.item([Current])
  327. end
  328. execute_killfocus: STRING
  329. do
  330. Result := cb_getfocus.item([Current])
  331. end
  332. execute_enterwindow: STRING
  333. do
  334. Result := cb_enterwindow.item([Current])
  335. end
  336. execute_leavewindow: STRING
  337. do
  338. Result := cb_leavewindow.item([Current])
  339. end
  340. execute_k_any (c: INTEGER): STRING
  341. do
  342. Result := cb_k_any.item([Current, c])
  343. end
  344. execute_help
  345. do
  346. cb_help.call([Current])
  347. end
  348. -- Extra
  349. execute_valuechanged: STRING
  350. do
  351. Result := cb_valuechanged.item([Current])
  352. end
  353. feature {}
  354. -- For callbacks
  355. cb_map: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  356. cb_unmap: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  357. cb_destroy: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  358. cb_getfocus: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  359. cb_killfocus: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  360. cb_enterwindow: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  361. cb_leavewindow: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  362. cb_k_any: FUNCTION[TUPLE[IUP_DATE_PICK, INTEGER], STRING]
  363. cb_help: PROCEDURE[TUPLE[IUP_DATE_PICK]]
  364. cb_valuechanged: FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  365. -- Internals
  366. int_date_pick: POINTER
  367. external "plug_in"
  368. alias "{
  369. location: "${sys}/plugins"
  370. module_name: "iup"
  371. feature_name: "IupDatePick()"
  372. }"
  373. end
  374. -- Validations
  375. is_valid_order (value: STRING): BOOLEAN
  376. do
  377. if value.is_equal("DMY") or
  378. value.is_equal("DYM") or
  379. value.is_equal("MYD") or
  380. value.is_equal("MDY") or
  381. value.is_equal("YMD") or
  382. value.is_equal("YDM") then
  383. Result := True
  384. else
  385. Result := False
  386. end
  387. end
  388. end
  389. -- The MIT License (MIT)
  390. -- Copyright (c) 2016, 2017 by German A. Arias
  391. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  392. -- of this software and associated documentation files (the "Software"), to deal
  393. -- in the Software without restriction, including without limitation the rights
  394. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  395. -- copies of the Software, and to permit persons to whom the Software is
  396. -- furnished to do so, subject to the following conditions:
  397. --
  398. -- The above copyright notice and this permission notice shall be included in
  399. -- all copies or substantial portions of the Software.
  400. --
  401. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  402. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  403. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  404. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  405. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  406. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  407. -- SOFTWARE.