iup_date_pick.e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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.out
  125. str.append_string("/")
  126. str.append_string(month.out)
  127. str.append_string("/")
  128. str.append_string(day.out)
  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. open_dropdow
  139. -- Open the dropdown calendar (don't work on Windows).
  140. do
  141. iup_open.set_attribute(Current, "SHOWDROPDOWN", "YES")
  142. end
  143. close_dropdow
  144. -- Close the dropdown calendar.
  145. do
  146. iup_open.set_attribute(Current, "SHOWDROPDOWN", "NO")
  147. end
  148. set_today
  149. -- Set value to today date.
  150. do
  151. iup_open.set_attribute(Current, "VALUE", "TODAY")
  152. end
  153. get_value: TUPLE[INTEGER, INTEGER, INTEGER]
  154. -- Return the selected date in format [year, month, day]".
  155. do
  156. Result := convert_date_to_tuple(get_string_value)
  157. end
  158. get_string_value: STRING
  159. -- Return the selected date in format "year/month/day".
  160. do
  161. Result := iup_open.get_attribute(Current, "VALUE")
  162. end
  163. set_zero_preced (state: BOOLEAN)
  164. -- Day and month numbers will be preceded by a zero. Must be set before
  165. -- ORDER in Windows. Default: False.
  166. do
  167. iup_open.set_attribute(Current, "ZEROPRECED",
  168. boolean_to_yesno(state))
  169. end
  170. -- Callbacks
  171. -- Common
  172. set_cb_map (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  173. -- Called right after an element is mapped and its attributes updated.
  174. local
  175. operation: INTEGER
  176. do
  177. cb_map := act
  178. if cb_map /= Void then
  179. operation := 1
  180. else
  181. operation := 0
  182. end
  183. iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
  184. end
  185. set_cb_unmap (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  186. -- Called right before an element is unmapped.
  187. local
  188. operation: INTEGER
  189. do
  190. cb_unmap := act
  191. if cb_unmap /= Void then
  192. operation := 1
  193. else
  194. operation := 0
  195. end
  196. iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
  197. end
  198. set_cb_destroy (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  199. -- Called right before an element is destroyed.
  200. local
  201. operation: INTEGER
  202. do
  203. cb_destroy := act
  204. if cb_destroy /= Void then
  205. operation := 1
  206. else
  207. operation := 0
  208. end
  209. iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
  210. end
  211. set_cb_get_focus (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  212. -- Action generated when an element is given keyboard focus.
  213. -- This callback is called after the KILLFOCUS_CB of the element
  214. -- that loosed the focus. The {IUP}.get_focus function during the
  215. -- callback returns the element that loosed the focus.
  216. local
  217. operation: INTEGER
  218. do
  219. cb_getfocus := act
  220. if cb_getfocus /= Void then
  221. operation := 1
  222. else
  223. operation := 0
  224. end
  225. iup_open.set_callback (Current, "GETFOCUS_CB", "NONEEDED", operation)
  226. end
  227. set_cb_kill_focus (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  228. -- Action generated when an element loses keyboard focus. This
  229. -- callback is called before the GETFOCUS_CB of the element that
  230. -- gets the focus.
  231. local
  232. operation: INTEGER
  233. do
  234. cb_killfocus := act
  235. if cb_killfocus /= Void then
  236. operation := 1
  237. else
  238. operation := 0
  239. end
  240. iup_open.set_callback (Current, "KILLFOCUS_CB", "NONEEDED", operation)
  241. end
  242. set_cb_enter_window (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  243. -- Action generated when the mouse enters the native element.
  244. local
  245. operation: INTEGER
  246. do
  247. cb_enterwindow := act
  248. if cb_enterwindow /= Void then
  249. operation := 1
  250. else
  251. operation := 0
  252. end
  253. iup_open.set_callback (Current, "ENTERWINDOW_CB", "NONEEDED", operation)
  254. end
  255. set_cb_leave_window (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  256. -- Action generated when the mouse leaves the native element.
  257. local
  258. operation: INTEGER
  259. do
  260. cb_leavewindow := act
  261. if cb_leavewindow /= Void then
  262. operation := 1
  263. else
  264. operation := 0
  265. end
  266. iup_open.set_callback (Current, "LEAVEWINDOW_CB", "NONEEDED", operation)
  267. end
  268. set_cb_k_any (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK, INTEGER], STRING])
  269. -- Action generated when a keyboard event occurs.
  270. -- IUP_WIDGET the element that activated the event.
  271. -- INTEGER identifier of typed key. Please refer to the Keyboard
  272. -- Codes table for a list of possible values.
  273. --
  274. -- Returns: If IUP_IGNORE is returned the key is ignored and not
  275. -- processed by the control and not propagated. If returns
  276. -- IUP_CONTINUE, the key will be processed and the event will be
  277. -- propagated to the parent of the element receiving it, this is
  278. -- the default behavior. If returns IUP_DEFAULT the key is processed
  279. -- but it is not propagated. IUP_CLOSE will be processed.
  280. local
  281. operation: INTEGER
  282. do
  283. cb_k_any := act
  284. if cb_k_any /= Void then
  285. operation := 1
  286. else
  287. operation := 0
  288. end
  289. iup_open.set_callback (Current, "K_ANY", "NONEEDED", operation)
  290. end
  291. set_cb_help (act: detachable PROCEDURE[TUPLE[IUP_DATE_PICK]])
  292. -- Action generated when the user press F1 at a control. In Motif
  293. -- is also activated by the Help button in some workstations
  294. -- keyboard.
  295. -- Returns: IUP_CLOSE will be processed.
  296. local
  297. operation: INTEGER
  298. do
  299. cb_help := act
  300. if cb_help /= Void then
  301. operation := 1
  302. else
  303. operation := 0
  304. end
  305. iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
  306. end
  307. -- Extra
  308. set_cb_value_changed (act: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING])
  309. local
  310. operation: INTEGER
  311. do
  312. cb_valuechanged := act
  313. if cb_valuechanged /= Void then
  314. operation := 1
  315. else
  316. operation := 0
  317. end
  318. iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
  319. end
  320. -- Validations
  321. is_valid_order (value: STRING): BOOLEAN
  322. do
  323. if value.is_equal("DMY") or
  324. value.is_equal("DYM") or
  325. value.is_equal("MYD") or
  326. value.is_equal("MDY") or
  327. value.is_equal("YMD") or
  328. value.is_equal("YDM") then
  329. Result := True
  330. else
  331. Result := False
  332. end
  333. end
  334. feature {IUP} -- Internal handle of callbacks
  335. -- Common callbacks
  336. execute_map: STRING
  337. do
  338. if attached cb_map as int_cb then
  339. Result := int_cb.item([Current])
  340. else
  341. Result := "IUP_DEFAULT"
  342. end
  343. end
  344. execute_unmap: STRING
  345. do
  346. if attached cb_unmap as int_cb then
  347. Result := int_cb.item([Current])
  348. else
  349. Result := "IUP_DEFAULT"
  350. end
  351. end
  352. execute_destroy: STRING
  353. do
  354. if attached cb_destroy as int_cb then
  355. Result := int_cb.item([Current])
  356. else
  357. Result := "IUP_DEFAULT"
  358. end
  359. end
  360. execute_getfocus: STRING
  361. do
  362. if attached cb_getfocus as int_cb then
  363. Result := int_cb.item([Current])
  364. else
  365. Result := "IUP_DEFAULT"
  366. end
  367. end
  368. execute_killfocus: STRING
  369. do
  370. if attached cb_killfocus as int_cb then
  371. Result := int_cb.item([Current])
  372. else
  373. Result := "IUP_DEFAULT"
  374. end
  375. end
  376. execute_enterwindow: STRING
  377. do
  378. if attached cb_enterwindow as int_cb then
  379. Result := int_cb.item([Current])
  380. else
  381. Result := "IUP_DEFAULT"
  382. end
  383. end
  384. execute_leavewindow: STRING
  385. do
  386. if attached cb_leavewindow as int_cb then
  387. Result := int_cb.item([Current])
  388. else
  389. Result := "IUP_DEFAULT"
  390. end
  391. end
  392. execute_k_any (c: INTEGER): STRING
  393. do
  394. if attached cb_k_any as int_cb then
  395. Result := int_cb.item([Current, c])
  396. else
  397. Result := "IUP_DEFAULT"
  398. end
  399. end
  400. execute_help
  401. do
  402. if attached cb_help as int_cb then
  403. int_cb.call([Current])
  404. end
  405. end
  406. -- Extra
  407. execute_valuechanged: STRING
  408. do
  409. if attached cb_valuechanged as int_cb then
  410. Result := int_cb.item([Current])
  411. else
  412. Result := "IUP_DEFAULT"
  413. end
  414. end
  415. feature {NONE}
  416. -- For callbacks
  417. cb_map: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  418. cb_unmap: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  419. cb_destroy: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  420. cb_getfocus: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  421. cb_killfocus: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  422. cb_enterwindow: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  423. cb_leavewindow: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  424. cb_k_any: detachable FUNCTION[TUPLE[IUP_DATE_PICK, INTEGER], STRING]
  425. cb_help: detachable PROCEDURE[TUPLE[IUP_DATE_PICK]]
  426. cb_valuechanged: detachable FUNCTION[TUPLE[IUP_DATE_PICK], STRING]
  427. -- Internals
  428. int_date_pick: POINTER
  429. external
  430. "C inline use %"eiffel-iup.h%""
  431. alias
  432. "return IupDatePick();"
  433. end
  434. end
  435. -- The MIT License (MIT)
  436. -- Copyright (c) 2016, 2017, 2019, 2020, 2021 by German A. Arias
  437. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  438. -- of this software and associated documentation files (the "Software"), to deal
  439. -- in the Software without restriction, including without limitation the rights
  440. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  441. -- copies of the Software, and to permit persons to whom the Software is
  442. -- furnished to do so, subject to the following conditions:
  443. --
  444. -- The above copyright notice and this permission notice shall be included in
  445. -- all copies or substantial portions of the Software.
  446. --
  447. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  448. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  449. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  450. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  451. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  452. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  453. -- SOFTWARE.