Fl_Time.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Fl_Time.cxx
  2. // Source file for the time widget class
  3. //
  4. // Copyright (C) 2000 Softfield Research Ltd.
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <FLU/Fl_Time.H>
  23. Fl_Time::Fl_Time(int x, int y, int w, int h, char *l) : Fl_Group(x, y, w, h, l) {
  24. int button_width = (int)(w/7);
  25. input_time = new Fl_Input(x, y, w - button_width * 4, h, 0);
  26. input_time->callback(input_changed_cb, this);
  27. input_time->when(FL_WHEN_CHANGED);
  28. input_time->textsize(10);
  29. button_decrease_hour = new Fl_Repeat_Button(x + w - 4 * button_width, y, button_width, h, "H-");
  30. button_decrease_hour->callback(button_cb, this);
  31. button_decrease_hour->labelsize(10);
  32. button_increase_hour = new Fl_Repeat_Button(x + w - 3 * button_width, y, button_width, h, "H+");
  33. button_increase_hour->callback(button_cb, this);
  34. button_increase_hour->labelsize(10);
  35. button_decrease_minute = new Fl_Repeat_Button(x + w - 2 * button_width, y, button_width, h, "M-");
  36. button_decrease_minute->callback(button_cb, this);
  37. button_decrease_minute->labelsize(10);
  38. button_increase_minute = new Fl_Repeat_Button(x + w - button_width, y, button_width, h, "M+");
  39. button_increase_minute->callback(button_cb, this);
  40. button_increase_minute->labelsize(10);
  41. end();
  42. type(FL_TIME_12HOUR);
  43. current_time();
  44. }
  45. void Fl_Time::current_time() {
  46. struct tm* display_time_tm;
  47. gettimeofday(&current_tv, 0);
  48. display_tv.tv_sec = current_tv.tv_sec;
  49. display_tv.tv_usec = current_tv.tv_usec;
  50. display_time_tm = localtime(&current_tv.tv_sec);
  51. if(type() == FL_TIME_24HOUR) strftime(time_string, 19, "%2H:%2M", display_time_tm);
  52. else strftime(time_string, 19, "%2I:%2M %p", display_time_tm);
  53. input_time->value(time_string);
  54. }
  55. void Fl_Time::refresh() {
  56. long different;
  57. struct tm *display_time_tm;
  58. if (valid()) {
  59. different = - display_tv.tv_sec + current_tv.tv_sec;
  60. gettimeofday(&current_tv, 0);
  61. display_tv.tv_sec = current_tv.tv_sec - different;
  62. display_time_tm = localtime(&display_tv.tv_sec);
  63. if(type() == FL_TIME_24HOUR) strftime(time_string, 19, "%2H:%2M", display_time_tm);
  64. else strftime(time_string, 19, "%2I:%2M %p", display_time_tm);
  65. input_time->value(time_string);
  66. }
  67. }
  68. int Fl_Time::hour() {
  69. struct tm *display_time_tm;
  70. display_time_tm = localtime(&display_tv.tv_sec);
  71. return display_time_tm->tm_hour;
  72. }
  73. int Fl_Time::minute() {
  74. struct tm *display_time_tm;
  75. display_time_tm = localtime(&display_tv.tv_sec);
  76. return display_time_tm->tm_min;
  77. }
  78. void Fl_Time::redisplay() {
  79. struct tm *display_time_tm;
  80. display_time_tm = localtime(&display_tv.tv_sec);
  81. if(type() == FL_TIME_24HOUR) strftime(time_string, 19, "%2H:%2M", display_time_tm);
  82. else strftime(time_string, 19, "%2I:%2M %p", display_time_tm);
  83. input_time->value(time_string);
  84. }
  85. void Fl_Time::hour(int value) {
  86. struct tm *display_time_tm;
  87. display_time_tm = localtime(&display_tv.tv_sec);
  88. display_time_tm->tm_hour = value;
  89. display_tv.tv_sec = mktime(display_time_tm);
  90. }
  91. void Fl_Time::minute(int value) {
  92. struct tm *display_time_tm;
  93. display_time_tm = localtime(&display_tv.tv_sec);
  94. if(value < 0) {
  95. display_time_tm->tm_min = 59;
  96. } else if(value >= 0 && value <= 59) {
  97. display_time_tm->tm_min = value;
  98. } else if (value > 59) {
  99. display_time_tm->tm_min = 0;
  100. }
  101. display_time_tm->tm_sec = 0;
  102. display_tv.tv_sec = mktime(display_time_tm);
  103. }
  104. void Fl_Time::value(int h, int m) {
  105. hour(h);
  106. minute(m);
  107. }
  108. bool Fl_Time::valid() {
  109. int h, m;
  110. char a[5];
  111. if(type() == FL_TIME_12HOUR) {
  112. if (sscanf(input_time->value(), "%d:%d %s",&h, &m, a) == 3) {
  113. if (h >= 1 && h <= 12 && m >= 0 && m <= 59 && (strcasecmp(a, "am") == 0 || strcasecmp(a, "pm") == 0)) {
  114. last_valid = true;
  115. return true;
  116. }
  117. }
  118. } else {
  119. if (sscanf(input_time->value(), "%d:%d",&h, &m) == 2) {
  120. if (h >= 0 && h <= 23 && m >= 0 && m <= 59) {
  121. last_valid = true;
  122. return true;
  123. }
  124. }
  125. }
  126. last_valid = false;
  127. return false;
  128. }
  129. void Fl_Time::input_changed_cb(Fl_Widget* widget, void* data) {
  130. Fl_Time* t = (Fl_Time*) data;
  131. int h, m;
  132. char a[5];
  133. if (t->valid()) {
  134. if(t->type() == FL_TIME_12HOUR) {
  135. sscanf(t->input_time->value(), "%d:%d %2s",&h, &m, a);
  136. if(strcasecmp(a, "am") == 0) {
  137. if(h < 12) {
  138. t->hour(h);
  139. } else {
  140. t->hour(0);
  141. }
  142. } else {
  143. if(h < 12) {
  144. t->hour(h + 12);
  145. } else {
  146. t->hour(12);
  147. }
  148. }
  149. } else {
  150. sscanf(t->input_time->value(), "%d:%d",&h, &m);
  151. t->hour(h);
  152. }
  153. t->minute(m);
  154. }
  155. t->do_callback();
  156. }
  157. void Fl_Time::button_cb(Fl_Widget* widget, void* data) {
  158. Fl_Time* t = (Fl_Time*) data;
  159. if(widget == t->button_decrease_hour) {
  160. t->hour(t->hour()-1);
  161. }
  162. if (widget == t->button_decrease_minute) {
  163. t->minute(t->minute()-1);
  164. }
  165. if (widget == t->button_increase_minute) {
  166. t->minute(t->minute()+1);
  167. }
  168. if (widget == t->button_increase_hour) {
  169. t->hour(t->hour()+1);
  170. }
  171. t->redisplay();
  172. t->do_callback();
  173. }
  174. void Fl_Time::textsize(int size) {
  175. input_time->textsize(size);
  176. }
  177. void Fl_Time::labelsize(int size) {
  178. button_decrease_hour->labelsize(size);
  179. button_decrease_minute->labelsize(size);
  180. button_increase_minute->labelsize(size);
  181. button_increase_hour->labelsize(size);
  182. Fl_Group::labelsize(size);
  183. }
  184. void Fl_Time::textfont(Fl_Font font) {
  185. input_time->textfont(font);
  186. }
  187. void Fl_Time::labelfont(Fl_Font font) {
  188. button_decrease_hour->labelfont(font);
  189. button_decrease_minute->labelfont(font);
  190. button_increase_minute->labelfont(font);
  191. button_increase_hour->labelfont(font);
  192. Fl_Group::labelfont(font);
  193. }
  194. void Fl_Time::textcolor(Fl_Color color) {
  195. input_time->textcolor(color);
  196. }
  197. void Fl_Time::labelcolor(Fl_Color color) {
  198. button_decrease_hour->labelcolor(color);
  199. button_decrease_minute->labelcolor(color);
  200. button_increase_minute->labelcolor(color);
  201. button_increase_hour->labelcolor(color);
  202. Fl_Group::labelcolor(color);
  203. }
  204. int Fl_Time::textsize() {
  205. return input_time->textsize();
  206. }
  207. int Fl_Time::labelsize() {
  208. return button_decrease_hour->labelsize();
  209. }
  210. Fl_Font Fl_Time::labelfont() {
  211. return button_decrease_hour->labelfont();
  212. }
  213. Fl_Font Fl_Time::textfont() {
  214. return input_time->textfont();
  215. }
  216. Fl_Color Fl_Time::labelcolor() {
  217. return button_decrease_hour->labelcolor();
  218. }
  219. Fl_Color Fl_Time::textcolor() {
  220. return input_time->textcolor();
  221. }