summaryrefslogtreecommitdiff
path: root/indra/llui/lltimectrl.cpp
blob: a77842a39205a4d199656001a1e6df0d6c3c3cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
 * @file lltimectrl.cpp
 * @brief LLTimeCtrl base class
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2011, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "lltimectrl.h"

#include "llui.h"
#include "lluiconstants.h"

#include "llbutton.h"
#include "llfontgl.h"
#include "lllineeditor.h"
#include "llkeyboard.h"
#include "llstring.h"
#include "lltextbox.h"
#include "lluictrlfactory.h"

static LLDefaultChildRegistry::Register<LLTimeCtrl> time_r("time");

const U32 AMPM_LEN = 3;
const U32 MINUTES_MIN = 0;
const U32 MINUTES_MAX = 59;
const U32 HOURS_MIN = 1;
const U32 HOURS_MAX = 12;

LLTimeCtrl::Params::Params()
:	label_width("label_width"),
	allow_text_entry("allow_text_entry", true),
	text_enabled_color("text_enabled_color"),
	text_disabled_color("text_disabled_color"),
	up_button("up_button"),
	down_button("down_button")
{}

LLTimeCtrl::LLTimeCtrl(const LLTimeCtrl::Params& p)
:	LLUICtrl(p),
	mLabelBox(NULL),
	mTextEnabledColor(p.text_enabled_color()),
	mTextDisabledColor(p.text_disabled_color()),
	mHours(HOURS_MIN),
	mMinutes(MINUTES_MIN)
{
	static LLUICachedControl<S32> spinctrl_spacing ("UISpinctrlSpacing", 0);
	static LLUICachedControl<S32> spinctrl_btn_width ("UISpinctrlBtnWidth", 0);
	static LLUICachedControl<S32> spinctrl_btn_height ("UISpinctrlBtnHeight", 0);
	S32 centered_top = getRect().getHeight();
	S32 centered_bottom = getRect().getHeight() - 2 * spinctrl_btn_height;
	S32 label_width = llclamp(p.label_width(), 0, llmax(0, getRect().getWidth() - 40));
	S32 editor_left = label_width + spinctrl_spacing;

	//================= Label =================//
	if( !p.label().empty() )
	{
		LLRect label_rect( 0, centered_top, label_width, centered_bottom );
		LLTextBox::Params params;
		params.name("TimeCtrl Label");
		params.rect(label_rect);
		params.initial_value(p.label());
		if (p.font.isProvided())
		{
			params.font(p.font);
		}
		mLabelBox = LLUICtrlFactory::create<LLTextBox> (params);
		addChild(mLabelBox);

		editor_left = label_rect.mRight + spinctrl_spacing;
	}

	S32 editor_right = getRect().getWidth() - spinctrl_btn_width - spinctrl_spacing;

	//================= Editor ================//
	LLRect editor_rect( editor_left, centered_top, editor_right, centered_bottom );
	LLLineEditor::Params params;
	params.name("SpinCtrl Editor");
	params.rect(editor_rect);
	if (p.font.isProvided())
	{
		params.font(p.font);
	}

	params.follows.flags(FOLLOWS_LEFT | FOLLOWS_BOTTOM);
	params.max_length.chars(8);
	params.keystroke_callback(boost::bind(&LLTimeCtrl::onTextEntry, this, _1));
	mEditor = LLUICtrlFactory::create<LLLineEditor> (params);
	mEditor->setPrevalidateInput(LLTextValidate::validateNonNegativeS32NoSpace);
	mEditor->setPrevalidate(boost::bind(&LLTimeCtrl::isTimeStringValid, this, _1));
	mEditor->setText(LLStringExplicit("0:00 AM"));
	addChild(mEditor);

	//================= Spin Buttons ==========//
	LLButton::Params up_button_params(p.up_button);
	up_button_params.rect = LLRect(editor_right + 1, getRect().getHeight(), editor_right + spinctrl_btn_width, getRect().getHeight() - spinctrl_btn_height);

	up_button_params.click_callback.function(boost::bind(&LLTimeCtrl::onUpBtn, this));
	up_button_params.mouse_held_callback.function(boost::bind(&LLTimeCtrl::onUpBtn, this));
	mUpBtn = LLUICtrlFactory::create<LLButton>(up_button_params);
	addChild(mUpBtn);

	LLButton::Params down_button_params(p.down_button);
	down_button_params.rect = LLRect(editor_right + 1, getRect().getHeight() - spinctrl_btn_height, editor_right + spinctrl_btn_width, getRect().getHeight() - 2 * spinctrl_btn_height);
	down_button_params.click_callback.function(boost::bind(&LLTimeCtrl::onDownBtn, this));
	down_button_params.mouse_held_callback.function(boost::bind(&LLTimeCtrl::onDownBtn, this));
	mDownBtn = LLUICtrlFactory::create<LLButton>(down_button_params);
	addChild(mDownBtn);

	setUseBoundingRect( TRUE );
}

BOOL LLTimeCtrl::handleKeyHere(KEY key, MASK mask)
{
	if (mEditor->hasFocus())
	{
		if(key == KEY_UP)
		{
			onUpBtn();
			return TRUE;
		}
		if(key == KEY_DOWN)
		{
			onDownBtn();
			return TRUE;
		}
	}
	return FALSE;
}

void LLTimeCtrl::onUpBtn()
{
	switch(getEditingPart())
	{
	case HOURS:
		increaseHours();
		break;
	case MINUTES:
		increaseMinutes();
		break;
	case DAYPART:
		switchDayPeriod();
		break;
	default:
		break;
	}

	buildTimeString();
	mEditor->setText(mTimeString);
}

void LLTimeCtrl::onDownBtn()
{
	switch(getEditingPart())
	{
	case HOURS:
		decreaseHours();
		break;
	case MINUTES:
		decreaseMinutes();
		break;
	case DAYPART:
		switchDayPeriod();
		break;
	default:
		break;
	}

	buildTimeString();
	mEditor->setText(mTimeString);
}

void LLTimeCtrl::onFocusLost()
{
	buildTimeString();
	mEditor->setText(mTimeString);

	LLUICtrl::onFocusLost();
}

void LLTimeCtrl::onTextEntry(LLLineEditor* line_editor)
{
	LLWString time_str = line_editor->getWText();
	switch(getEditingPart())
	{
	case HOURS:
		validateHours(getHoursWString(time_str));
		break;
	case MINUTES:
		validateMinutes(getMinutesWString(time_str));
		break;
	default:
		break;
	}
}

bool LLTimeCtrl::isTimeStringValid(const LLWString &wstr)
{
	if (!isHoursStringValid(getHoursWString(wstr)) || !isMinutesStringValid(getMinutesWString(wstr)) || !isPMAMStringValid(wstr))
		return false;

	return true;
}

bool LLTimeCtrl::isHoursStringValid(const LLWString& wstr)
{
	U32 hours;
	std::string utf8time = wstring_to_utf8str(wstr);
	if ((!LLStringUtil::convertToU32(utf8time, hours) || (hours <= HOURS_MAX)) && wstr.length() < 3)
		return true;

	return false;
}

bool LLTimeCtrl::isMinutesStringValid(const LLWString& wstr)
{
	U32 minutes;
	std::string utf8time = wstring_to_utf8str(wstr);
	if (!LLStringUtil::convertToU32(utf8time, minutes) || (minutes <= MINUTES_MAX) && wstr.length() < 3)
		return true;

	return false;
}

void LLTimeCtrl::validateHours(const LLWString& wstr)
{
	U32 hours;
	std::string utf8time = wstring_to_utf8str(wstr);
	if (LLStringUtil::convertToU32(utf8time, hours) && (hours >= HOURS_MIN) && (hours <= HOURS_MAX))
	{
		mHours = hours;
	}
	else
	{
		mHours = HOURS_MIN;
	}
}

void LLTimeCtrl::validateMinutes(const LLWString& wstr)
{
	U32 minutes;
	std::string utf8time = wstring_to_utf8str(wstr);
	if (LLStringUtil::convertToU32(utf8time, minutes) && (minutes >= MINUTES_MIN) && (minutes <= MINUTES_MAX))
	{
		mMinutes = minutes;
	}
	else
	{
		mMinutes = MINUTES_MIN;
	}
}

bool LLTimeCtrl::isPMAMStringValid(const LLWString &wstr)
{
	S32 len = wstr.length();

	bool valid = (wstr[--len] == 'M') && (wstr[--len] == 'P' || wstr[len] == 'A');

	return valid;
}

LLWString LLTimeCtrl::getHoursWString(const LLWString& wstr)
{
	size_t colon_index = wstr.find_first_of(':');
	LLWString hours_str = wstr.substr(0, colon_index);

	return hours_str;
}

LLWString LLTimeCtrl::getMinutesWString(const LLWString& wstr)
{
	size_t colon_index = wstr.find_first_of(':');
	++colon_index;

	int minutes_len = wstr.length() - colon_index - AMPM_LEN;
	LLWString minutes_str = wstr.substr(colon_index, minutes_len);

	return minutes_str;
}

void LLTimeCtrl::increaseMinutes()
{
	if (++mMinutes > MINUTES_MAX)
	{
		mMinutes = MINUTES_MIN;
	}
}

void LLTimeCtrl::increaseHours()
{
	if (++mHours > HOURS_MAX)
	{
		mHours = HOURS_MIN;
	}
}

void LLTimeCtrl::decreaseMinutes()
{
	if (mMinutes-- == MINUTES_MIN)
	{
		mMinutes = MINUTES_MAX;
	}
}

void LLTimeCtrl::decreaseHours()
{
	if (mHours-- == HOURS_MIN)
	{
		mHours = HOURS_MAX;
		switchDayPeriod();
	}
}

void LLTimeCtrl::switchDayPeriod()
{
	switch (mCurrentDayPeriod)
	{
	case AM:
		mCurrentDayPeriod = PM;
		break;
	case PM:
		mCurrentDayPeriod = AM;
		break;
	}
}

void LLTimeCtrl::buildTimeString()
{
	std::stringstream time_buf;
	time_buf << mHours << ":";

	if (mMinutes < 10)
	{
		time_buf << "0";
	}

	time_buf << mMinutes;
	time_buf << " ";

	switch (mCurrentDayPeriod)
	{
	case AM:
		time_buf << "AM";
		break;
	case PM:
		time_buf << "PM";
		break;
	}

	mTimeString = time_buf.str();
}

LLTimeCtrl::EEditingPart LLTimeCtrl::getEditingPart()
{
	S32 cur_pos = mEditor->getCursor();
	LLWString time_str = mEditor->getWText();

	S32 colon_index = time_str.find_first_of(':');

	if (cur_pos <= colon_index)
	{
		return HOURS;
	}
	else if (cur_pos > colon_index && cur_pos <= (S32)(time_str.length() - AMPM_LEN))
	{
		return MINUTES;
	}
	else if (cur_pos > (S32)(time_str.length() - AMPM_LEN))
	{
		return DAYPART;
	}

	return NONE;
}