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"
26 TimeEntry::TimeEntry(BC_WindowBase *gui, int x, int y,
27 int *output_day, double *output_time,
28 int time_format, int time_w)
33 this->output_day = output_day;
34 this->output_time = output_time;
35 this->time_format = time_format;
36 this->time_w = time_w;
43 TimeEntry::~TimeEntry()
49 const char* TimeEntry::day_table[] =
51 N_("Sun"), N_("Mon"), N_("Tue"), N_("Wed"), N_("Thu"), N_("Fri"), N_("Sat"), "*"
54 int TimeEntry::day_to_int(const char *day)
56 for(int i = 0; i < TOTAL_DAYS; i++)
57 if(!strcasecmp(day, _(day_table[i]))) return i;
62 void TimeEntry::time_to_hours(char *result, double time)
64 int hour = (int)(time / 3600);
65 sprintf(result, "%d", hour);
68 void TimeEntry::time_to_minutes(char *result, double time)
70 int hour = (int)(time / 3600);
71 int minute = (int)(time / 60 - hour * 60);
72 sprintf(result, "%d", minute);
75 void TimeEntry::time_to_seconds(char *result, double time)
77 int hour = (int)(time / 3600);
78 int minute = (int)(time / 60 - hour * 60);
79 float second = (float)time - (int64_t)hour * 3600 - (int64_t)minute * 60;
80 sprintf(result, "%.3f", second);
83 void TimeEntry::create_objects()
87 int time_w = this->time_w;
88 char string[BCTEXTLEN];
91 day_text = new DayText(this, x, y, 50,
92 day_table, TOTAL_DAYS, day_table[*output_day]);
93 gui->add_subwindow(day_text);
94 x += day_text->get_w();
95 time_w -= day_text->get_w();
96 day_tumbler = new DayTumbler(this, day_text, x, y);
97 gui->add_subwindow(day_tumbler);
98 x += day_tumbler->get_w();
99 time_w -= day_tumbler->get_w();
102 time_text = new TimeTextBox(this, x, y, time_w,
103 Units::totext(string, *output_time, time_format));
104 gui->add_subwindow(time_text);
106 time_text->set_separators(Units::format_to_separators(time_format));
109 void TimeEntry::enable()
112 if( day_text ) day_text->enable();
116 void TimeEntry::disable()
119 if( day_text ) day_text->disable();
120 time_text->disable();
123 int TimeEntry::get_h()
125 return time_text->get_h();
128 int TimeEntry::get_w()
131 if(day_text) w += day_text->get_w();
132 if(day_tumbler) w += day_tumbler->get_w();
133 w += time_text->get_w();
137 void TimeEntry::reposition_window(int x, int y)
139 int time_w = this->time_w;
144 day_text->reposition_window(x, y);
145 x += day_text->get_w();
146 time_w -= day_text->get_w();
149 day_tumbler->reposition_window(x, y);
150 x += day_tumbler->get_w();
151 time_w -= day_tumbler->get_w();
154 time_text->reposition_window(x, y, time_w);
157 void TimeEntry::update_day(int day)
159 if( !output_day ) return;
161 day_text->update(day_table[day]);
164 void TimeEntry::update_time(double time)
167 char text[BCTEXTLEN];
168 Units::totext(text, time, time_format);
169 time_text->update(text);
172 void TimeEntry::update(int *day, double *time)
178 day_text->update(day_table[*day]);
180 char text[BCTEXTLEN];
181 Units::totext(text, *time, time_format);
182 time_text->update(text);
185 int TimeEntry::handle_event()
192 DayText::DayText(TimeEntry *timeentry,
194 const char **table, int table_items,
196 : BC_TextBox(x, y, w, 1, _(text))
198 this->timeentry = timeentry;
200 this->table_items = table_items;
203 int DayText::handle_event()
205 *timeentry->output_day = TimeEntry::day_to_int(get_text());
206 return timeentry->handle_event();
210 DayTumbler::DayTumbler(TimeEntry *timeentry, DayText *text, int x, int y)
213 this->timeentry = timeentry;
218 int DayTumbler::handle_up_event()
220 if( !timeentry->enabled ) return 1;
221 *timeentry->output_day += 1;
222 if(*timeentry->output_day >= text->table_items)
223 *timeentry->output_day = 0;
224 text->update(_(text->table[*timeentry->output_day]));
225 return timeentry->handle_event();
228 int DayTumbler::handle_down_event()
230 if( !timeentry->enabled ) return 1;
231 *timeentry->output_day -= 1;
232 if(*timeentry->output_day < 0)
233 *timeentry->output_day = text->table_items - 1;
234 text->update(_(text->table[*timeentry->output_day]));
235 return timeentry->handle_event();
240 TimeEntryTumbler::TimeEntryTumbler(BC_WindowBase *gui, int x, int y,
241 int *output_day, double *output_time, int incr,
242 int time_format, int time_w)
243 : TimeEntry(gui, x, y, output_day, output_time,
244 time_format, time_w-BC_Tumbler::calculate_w())
246 this->time_w = time_w;
251 TimeEntryTumbler::~TimeEntryTumbler()
256 void TimeEntryTumbler::create_objects()
258 TimeEntry::create_objects();
259 sec_tumbler = new SecTumbler(this, x+TimeEntry::time_w, y);
260 gui->add_subwindow(sec_tumbler);
263 void TimeEntryTumbler::reposition_window(int x, int y)
265 TimeEntry::reposition_window(x, y);
266 sec_tumbler->reposition_window(x+TimeEntry::time_w, y);
269 int TimeEntryTumbler::get_h()
271 return TimeEntry::get_h();
274 int TimeEntryTumbler::get_w()
276 return TimeEntry::get_w() + BC_Tumbler::calculate_w();
279 int TimeEntryTumbler::handle_up_event()
281 if( !enabled ) return 1;
283 *output_time += incr;
284 char text[BCTEXTLEN];
285 Units::totext(text, *output_time, time_format);
286 time_text->update(text);
288 return handle_event();
291 int TimeEntryTumbler::handle_down_event()
293 if( !enabled ) return 1;
295 *output_time -= incr;
296 char text[BCTEXTLEN];
297 Units::totext(text, *output_time, time_format);
298 time_text->update(text);
300 return handle_event();
304 SecTumbler::SecTumbler(TimeEntryTumbler *timetumbler, int x, int y)
307 this->timetumbler = timetumbler;
310 int SecTumbler::handle_up_event()
312 return timetumbler->handle_up_event();
315 int SecTumbler::handle_down_event()
317 return timetumbler->handle_down_event();
322 TimeTextBox::TimeTextBox(TimeEntry *timeentry,
323 int x, int y, int w, char *default_text)
324 : BC_TextBox(x, y, w, 1, default_text)
326 this->timeentry = timeentry;
329 int TimeTextBox::handle_event()
331 *timeentry->output_time = Units::text_to_seconds(get_text(),
332 48000, timeentry->time_format);
333 return timeentry->handle_event();