4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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
22 #include "timeentry.h"
25 TimeEntry::TimeEntry(BC_WindowBase *gui, int x, int y,
26 int *output_day, double *output_time,
27 int time_format, int time_w)
32 this->output_day = output_day;
33 this->output_time = output_time;
34 this->time_format = time_format;
35 this->time_w = time_w;
42 TimeEntry::~TimeEntry()
48 const char* TimeEntry::day_table[] =
50 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "*"
53 int TimeEntry::day_to_int(const char *day)
55 for(int i = 0; i < TOTAL_DAYS; i++)
56 if(!strcasecmp(day, day_table[i])) return i;
61 void TimeEntry::time_to_hours(char *result, double time)
63 int hour = (int)(time / 3600);
64 sprintf(result, "%d", hour);
67 void TimeEntry::time_to_minutes(char *result, double time)
69 int hour = (int)(time / 3600);
70 int minute = (int)(time / 60 - hour * 60);
71 sprintf(result, "%d", minute);
74 void TimeEntry::time_to_seconds(char *result, double time)
76 int hour = (int)(time / 3600);
77 int minute = (int)(time / 60 - hour * 60);
78 float second = (float)time - (int64_t)hour * 3600 - (int64_t)minute * 60;
79 sprintf(result, "%.3f", second);
82 void TimeEntry::create_objects()
86 int time_w = this->time_w;
87 char string[BCTEXTLEN];
90 day_text = new DayText(this, x, y, 50,
91 day_table, TOTAL_DAYS, day_table[*output_day]);
92 gui->add_subwindow(day_text);
93 x += day_text->get_w();
94 time_w -= day_text->get_w();
95 day_tumbler = new DayTumbler(this, day_text, x, y);
96 gui->add_subwindow(day_tumbler);
97 x += day_tumbler->get_w();
98 time_w -= day_tumbler->get_w();
101 time_text = new TimeTextBox(this, x, y, time_w,
102 Units::totext(string, *output_time, time_format));
103 gui->add_subwindow(time_text);
105 time_text->set_separators(Units::format_to_separators(time_format));
108 void TimeEntry::enable()
111 if( day_text ) day_text->enable();
115 void TimeEntry::disable()
118 if( day_text ) day_text->disable();
119 time_text->disable();
122 int TimeEntry::get_h()
124 return time_text->get_h();
127 int TimeEntry::get_w()
130 if(day_text) w += day_text->get_w();
131 if(day_tumbler) w += day_tumbler->get_w();
132 w += time_text->get_w();
136 void TimeEntry::reposition_window(int x, int y)
138 int time_w = this->time_w;
143 day_text->reposition_window(x, y);
144 x += day_text->get_w();
145 time_w -= day_text->get_w();
148 day_tumbler->reposition_window(x, y);
149 x += day_tumbler->get_w();
150 time_w -= day_tumbler->get_w();
153 time_text->reposition_window(x, y, time_w);
156 void TimeEntry::update_day(int day)
158 if( !output_day ) return;
160 day_text->update(day_table[day]);
163 void TimeEntry::update_time(double time)
166 char text[BCTEXTLEN];
167 Units::totext(text, time, time_format);
168 time_text->update(text);
171 void TimeEntry::update(int *day, double *time)
177 day_text->update(day_table[*day]);
179 char text[BCTEXTLEN];
180 Units::totext(text, *time, time_format);
181 time_text->update(text);
184 int TimeEntry::handle_event()
191 DayText::DayText(TimeEntry *timeentry,
193 const char **table, int table_items,
195 : BC_TextBox(x, y, w, 1, text)
197 this->timeentry = timeentry;
199 this->table_items = table_items;
202 int DayText::handle_event()
204 *timeentry->output_day = TimeEntry::day_to_int(get_text());
205 return timeentry->handle_event();
209 DayTumbler::DayTumbler(TimeEntry *timeentry, DayText *text, int x, int y)
212 this->timeentry = timeentry;
217 int DayTumbler::handle_up_event()
219 if( !timeentry->enabled ) return 1;
220 *timeentry->output_day += 1;
221 if(*timeentry->output_day >= text->table_items)
222 *timeentry->output_day = 0;
223 text->update(text->table[*timeentry->output_day]);
224 return timeentry->handle_event();
227 int DayTumbler::handle_down_event()
229 if( !timeentry->enabled ) return 1;
230 *timeentry->output_day -= 1;
231 if(*timeentry->output_day < 0)
232 *timeentry->output_day = text->table_items - 1;
233 text->update(text->table[*timeentry->output_day]);
234 return timeentry->handle_event();
239 TimeEntryTumbler::TimeEntryTumbler(BC_WindowBase *gui, int x, int y,
240 int *output_day, double *output_time, int incr,
241 int time_format, int time_w)
242 : TimeEntry(gui, x, y, output_day, output_time,
243 time_format, time_w-BC_Tumbler::calculate_w())
245 this->time_w = time_w;
250 TimeEntryTumbler::~TimeEntryTumbler()
255 void TimeEntryTumbler::create_objects()
257 TimeEntry::create_objects();
258 sec_tumbler = new SecTumbler(this, x+TimeEntry::time_w, y);
259 gui->add_subwindow(sec_tumbler);
262 void TimeEntryTumbler::reposition_window(int x, int y)
264 TimeEntry::reposition_window(x, y);
265 sec_tumbler->reposition_window(x+TimeEntry::time_w, y);
268 int TimeEntryTumbler::get_h()
270 return TimeEntry::get_h();
273 int TimeEntryTumbler::get_w()
275 return TimeEntry::get_w() + BC_Tumbler::calculate_w();
278 int TimeEntryTumbler::handle_up_event()
280 if( !enabled ) return 1;
282 *output_time += incr;
283 char text[BCTEXTLEN];
284 Units::totext(text, *output_time, time_format);
285 time_text->update(text);
287 return handle_event();
290 int TimeEntryTumbler::handle_down_event()
292 if( !enabled ) return 1;
294 *output_time -= incr;
295 char text[BCTEXTLEN];
296 Units::totext(text, *output_time, time_format);
297 time_text->update(text);
299 return handle_event();
303 SecTumbler::SecTumbler(TimeEntryTumbler *timetumbler, int x, int y)
306 this->timetumbler = timetumbler;
309 int SecTumbler::handle_up_event()
311 return timetumbler->handle_up_event();
314 int SecTumbler::handle_down_event()
316 return timetumbler->handle_down_event();
321 TimeTextBox::TimeTextBox(TimeEntry *timeentry,
322 int x, int y, int w, char *default_text)
323 : BC_TextBox(x, y, w, 1, default_text)
325 this->timeentry = timeentry;
328 int TimeTextBox::handle_event()
330 *timeentry->output_time = Units::text_to_seconds(get_text(),
331 48000, timeentry->time_format);
332 return timeentry->handle_event();