iup_timer.e 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. class IUP_TIMER
  2. -- Creates a timer which periodically invokes a callback when the time is
  3. -- up. Each timer should be destroyed using "destroy".
  4. -- In GTK uses g_timeout_add, in Windows uses SetTimer, and in Motif
  5. -- uses XtAppAddTimeOut.
  6. inherit
  7. ANY
  8. IUP_WIDGET
  9. export
  10. {NONE} all
  11. {ANY} destroy
  12. redefine
  13. execute_action
  14. end
  15. create {ANY}
  16. timer
  17. feature {ANY}
  18. timer
  19. local
  20. a_timer: POINTER
  21. do
  22. a_timer := int_timer
  23. set_widget(a_timer)
  24. end
  25. set_time (time: INTEGER)
  26. -- The time interval in milliseconds. In Windows the minimum
  27. -- value is 10ms.
  28. require
  29. time > 0
  30. do
  31. iup_open.set_attribute(Current, "TIME", time.out)
  32. end
  33. start
  34. do
  35. iup_open.set_attribute(Current, "RUN", "YES")
  36. end
  37. stop
  38. do
  39. iup_open.set_attribute(Current, "RUN", "NO")
  40. end
  41. is_running: BOOLEAN
  42. local
  43. str: STRING
  44. do
  45. str := iup_open.get_attribute(Current, "RUN")
  46. if str.is_equal("YES") then
  47. Result := True
  48. else
  49. Result := False
  50. end
  51. end
  52. get_elapsed_time: INTEGER
  53. -- Inside the callback returns the time elapsed since
  54. -- the timer was started.
  55. local
  56. str: STRING
  57. do
  58. str := iup_open.get_attribute(Current, "ELAPSEDTIME")
  59. Result := str.to_integer
  60. end
  61. -- Callback
  62. set_cb_action (act: detachable FUNCTION[TUPLE[IUP_TIMER], STRING])
  63. -- Called every time the defined time interval is reached.
  64. -- To stop the callback from being called simply stop de timer. Inside
  65. -- the callback "get_elapsed_time" returns the time elapsed since
  66. -- the timer was started.
  67. --
  68. -- ih: element that activated the event.
  69. --
  70. -- Returns: "IUP_CLOSE" will be processed.
  71. local
  72. operation: INTEGER
  73. do
  74. cb_action := act
  75. if cb_action /= Void then
  76. operation := 1
  77. else
  78. operation := 0
  79. end
  80. iup_open.set_callback (Current, "ACTION_CB", "Fn", operation)
  81. end
  82. feature {ANY}
  83. execute_action: STRING
  84. do
  85. if attached cb_action as int_cb then
  86. Result := int_cb.item([Current])
  87. else
  88. Result := "IUP_DEFAULT"
  89. end
  90. end
  91. feature {NONE}
  92. cb_action: detachable FUNCTION[TUPLE[IUP_TIMER], STRING]
  93. -- Internal
  94. int_timer: POINTER
  95. external
  96. "C inline use %"eiffel-iup.h%""
  97. alias
  98. "return IupTimer();"
  99. end
  100. end
  101. -- The MIT License (MIT)
  102. -- Copyright (c) 2019, 2020 by German A. Arias
  103. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  104. -- of this software and associated documentation files (the "Software"), to deal
  105. -- in the Software without restriction, including without limitation the rights
  106. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  107. -- copies of the Software, and to permit persons to whom the Software is
  108. -- furnished to do so, subject to the following conditions:
  109. --
  110. -- The above copyright notice and this permission notice shall be included in
  111. -- all copies or substantial portions of the Software.
  112. --
  113. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  114. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  115. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  116. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  117. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  118. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  119. -- SOFTWARE.