summaryrefslogtreecommitdiff
path: root/indra/newview/llscreenchannel.cpp
blob: 082bba027ffe1c40a353a1814027c771eb52a35f (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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/** 
 * @file llscreenchannel.cpp
 * @brief Class implements a channel on a screen in which appropriate toasts may appear.
 *
 * $LicenseInfo:firstyear=2000&license=viewergpl$
 * 
 * Copyright (c) 2000-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */


#include "llviewerprecompiledheaders.h" // must be first include

#include "lliconctrl.h"
#include "lltextbox.h"
#include "llscreenchannel.h"

#include "lltoastpanel.h"
#include "llviewercontrol.h"
#include "llviewerwindow.h"
#include "llfloaterreg.h"
#include "lltrans.h"

#include "lldockablefloater.h"
#include "llimpanel.h"
#include "llsyswellwindow.h"
#include "llimfloater.h"

#include <algorithm>

using namespace LLNotificationsUI;

bool LLScreenChannel::mWasStartUpToastShown = false;

//--------------------------------------------------------------------------
LLScreenChannel::LLScreenChannel(LLUUID& id):	mOverflowToastPanel(NULL), mStartUpToastPanel(NULL),
												mToastAlignment(NA_BOTTOM), mCanStoreToasts(true),
												mHiddenToastsNum(0), mOverflowToastHidden(false),
												mIsHovering(false), mControlHovering(false),
												mShowToasts(true)
{	
	mID = id;
	mOverflowFormatString = LLTrans::getString("OverflowInfoChannelString");
	mWorldViewRectConnection = gViewerWindow->setOnWorldViewRectUpdated(boost::bind(&LLScreenChannel::updatePositionAndSize, this, _1, _2));
	setMouseOpaque( false );
	setVisible(FALSE);
}

//--------------------------------------------------------------------------
void LLScreenChannel::init(S32 channel_left, S32 channel_right)
{
	S32 channel_top = gViewerWindow->getWorldViewRect().getHeight();
	S32 channel_bottom = gViewerWindow->getWorldViewRect().mBottom + gSavedSettings.getS32("ChannelBottomPanelMargin");
	setRect(LLRect(channel_left, channel_top, channel_right, channel_bottom));
	setVisible(TRUE);
}

//--------------------------------------------------------------------------
LLScreenChannel::~LLScreenChannel() 
{
	mWorldViewRectConnection.disconnect();
}

//--------------------------------------------------------------------------
void LLScreenChannel::updatePositionAndSize(LLRect old_world_rect, LLRect new_world_rect)
{
	S32 top_delta = old_world_rect.mTop - new_world_rect.mTop;
	S32 right_delta = old_world_rect.mRight - new_world_rect.mRight;

	LLRect this_rect = getRect();

	this_rect.mTop -= top_delta;
	switch(mChannelAlignment)
	{
	case CA_LEFT :
		break;
	case CA_CENTRE :
		this_rect.setCenterAndSize(new_world_rect.getWidth() / 2, new_world_rect.getHeight() / 2, this_rect.getWidth(), this_rect.getHeight());
		break;
	case CA_RIGHT :
		this_rect.mLeft -= right_delta;
		this_rect.mRight -= right_delta;
	}
	setRect(this_rect);
	redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::addToast(LLToast::Params p)
{
	bool store_toast = false, show_toast = false;

	show_toast = mWasStartUpToastShown && (mShowToasts || p.force_show);
	store_toast = !show_toast && p.can_be_stored && mCanStoreToasts;

	if(!show_toast && !store_toast)
	{
		mRejectToastSignal(p.notif_id);
		return;
	}

	ToastElem new_toast_elem(p);

	mOverflowToastHidden = false;
	
	new_toast_elem.toast->setOnFadeCallback(boost::bind(&LLScreenChannel::onToastFade, this, _1));
	new_toast_elem.toast->setOnToastDestroyedCallback(boost::bind(&LLScreenChannel::onToastDestroyed, this, _1));
	if(mControlHovering)
	{
		new_toast_elem.toast->setOnToastHoverCallback(boost::bind(&LLScreenChannel::onToastHover, this, _1, _2));
	}
	
	if(show_toast)
	{
		mToastList.push_back(new_toast_elem);
		redrawToasts();
	}	
	else // store_toast
	{
		mHiddenToastsNum++;
		storeToast(new_toast_elem);
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::onToastDestroyed(LLToast* toast)
{	
	std::vector<ToastElem>::iterator it = find(mToastList.begin(), mToastList.end(), static_cast<LLPanel*>(toast));
		
	if(it != mToastList.end())
	{
		mToastList.erase(it);
	}
}


//--------------------------------------------------------------------------
void LLScreenChannel::onToastFade(LLToast* toast)
{	
	std::vector<ToastElem>::iterator it = find(mToastList.begin(), mToastList.end(), static_cast<LLPanel*>(toast));
		
	bool delete_toast = !mCanStoreToasts || !toast->getCanBeStored();
	if(delete_toast)
	{
		mToastList.erase(it);
		deleteToast(toast);
	}
	else
	{
		storeToast((*it));
		mToastList.erase(it);
	}	

	redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::deleteToast(LLToast* toast)
{
	// send signal to observers about destroying of a toast
	toast->mOnDeleteToastSignal(toast);
	
	// update channel's Hovering state
	// turning hovering off mannualy because onMouseLeave won't happen if a toast was closed using a keyboard
	if(toast->hasFocus())
		setHovering(false);

	// close the toast
	toast->closeFloater();
}

//--------------------------------------------------------------------------

void LLScreenChannel::storeToast(ToastElem& toast_elem)
{
	// do not store clones
	std::vector<ToastElem>::iterator it = find(mStoredToastList.begin(), mStoredToastList.end(), toast_elem.id);
	if( it != mStoredToastList.end() )
		return;

	toast_elem.toast->stopTimer();
	mStoredToastList.push_back(toast_elem);
	mOnStoreToast(toast_elem.toast->getPanel(), toast_elem.id);
}

//--------------------------------------------------------------------------
void LLScreenChannel::loadStoredToastsToChannel()
{
	std::vector<ToastElem>::iterator it;

	if(mStoredToastList.size() == 0)
		return;
	
	mOverflowToastHidden = false;

	for(it = mStoredToastList.begin(); it != mStoredToastList.end(); ++it)
	{
		(*it).toast->resetTimer();
		mToastList.push_back((*it));
	}

	mStoredToastList.clear();
	redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::loadStoredToastByNotificationIDToChannel(LLUUID id)
{
	std::vector<ToastElem>::iterator it = find(mStoredToastList.begin(), mStoredToastList.end(), id);

	if( it == mStoredToastList.end() )
		return;

	mOverflowToastHidden = false;

	LLToast* toast = (*it).toast;
	toast->resetTimer();
	mToastList.push_back((*it));
	mStoredToastList.erase(it);

	redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::removeStoredToastByNotificationID(LLUUID id)
{
	// *TODO: may be remove this function
	std::vector<ToastElem>::iterator it = find(mStoredToastList.begin(), mStoredToastList.end(), id);

	if( it == mStoredToastList.end() )
		return;

	LLToast* toast = (*it).toast;
	mStoredToastList.erase(it);
	mRejectToastSignal(toast->getNotificationID());
}

//--------------------------------------------------------------------------
void LLScreenChannel::killToastByNotificationID(LLUUID id)
{
	// searching among toasts on a screen
	std::vector<ToastElem>::iterator it = find(mToastList.begin(), mToastList.end(), id);
	
	if( it != mToastList.end())
	{
		LLToast* toast = (*it).toast;
		// if it is a notification toast and notification is UnResponded - then respond on it
		// else - simply destroy a toast
		//
		// NOTE:	if a notification is unresponded this function will be called twice for the same toast.
		//			At first, the notification will be discarded, at second (it will be caused by discarding),
		//			the toast will be destroyed.
		if(toast->isNotificationValid())
		{
			mRejectToastSignal(toast->getNotificationID());
		}
		else
		{
			mToastList.erase(it);
			deleteToast(toast);
			redrawToasts();
		}
		return;
	}

	// searching among stored toasts
	it = find(mStoredToastList.begin(), mStoredToastList.end(), id);

	if( it != mStoredToastList.end() )
	{
		LLToast* toast = (*it).toast;
		mStoredToastList.erase(it);
		// send signal to a listener to let him perform some action on toast rejecting
		mRejectToastSignal(toast->getNotificationID());
		deleteToast(toast);
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::modifyToastByNotificationID(LLUUID id, LLPanel* panel)
{
	std::vector<ToastElem>::iterator it = find(mToastList.begin(), mToastList.end(), id);
	
	if( it != mToastList.end() && panel)
	{
		LLToast* toast = (*it).toast;
		LLPanel* old_panel = toast->getPanel();
		toast->removeChild(old_panel);
		delete old_panel;
		toast->insertPanel(panel);
		toast->resetTimer();
		redrawToasts();
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::redrawToasts()
{
	if(mToastList.size() == 0 || mIsHovering)
		return;

	hideToastsFromScreen();

	switch(mToastAlignment)
	{
	case NA_TOP : 
		showToastsTop();
		break;

	case NA_CENTRE :
		showToastsCentre();
		break;

	case NA_BOTTOM :
		showToastsBottom();					
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::showToastsBottom()
{
	LLRect	toast_rect;	
	S32		bottom = getRect().mBottom;
	std::vector<ToastElem>::reverse_iterator it;

	for(it = mToastList.rbegin(); it != mToastList.rend(); ++it)
	{
		if(it != mToastList.rbegin())
		{
			bottom = (*(it-1)).toast->getRect().mTop;
		}

		toast_rect = (*it).toast->getRect();
		toast_rect.setLeftTopAndSize(getRect().mLeft, bottom + toast_rect.getHeight()+gSavedSettings.getS32("ToastMargin"), toast_rect.getWidth() ,toast_rect.getHeight());
		(*it).toast->setRect(toast_rect);

		bool stop_showing_toasts = (*it).toast->getRect().mTop > getRect().getHeight();

		if(!stop_showing_toasts)
		{
			if( it != mToastList.rend()-1)
			{
				stop_showing_toasts = ((*it).toast->getRect().mTop + gSavedSettings.getS32("OverflowToastHeight") + gSavedSettings.getS32("ToastMargin")) > getRect().getHeight();
			}
		} 

		if(stop_showing_toasts)
			break;

		(*it).toast->setVisible(TRUE);	
	}

	if(it != mToastList.rend() && !mOverflowToastHidden)
	{
		mHiddenToastsNum = 0;
		for(; it != mToastList.rend(); it++)
		{
			(*it).toast->stopTimer();
			mHiddenToastsNum++;
		}
		createOverflowToast(bottom, gSavedSettings.getS32("NotificationToastTime"));
	}	
}

//--------------------------------------------------------------------------
void LLScreenChannel::showToastsCentre()
{
	LLRect	toast_rect;	
	S32		bottom = (getRect().mTop - getRect().mBottom)/2 + mToastList[0].toast->getRect().getHeight()/2;
	std::vector<ToastElem>::reverse_iterator it;

	for(it = mToastList.rbegin(); it != mToastList.rend(); ++it)
	{
		toast_rect = (*it).toast->getRect();
		toast_rect.setLeftTopAndSize(getRect().mLeft - toast_rect.getWidth() / 2, bottom + toast_rect.getHeight() / 2 + gSavedSettings.getS32("ToastMargin"), toast_rect.getWidth() ,toast_rect.getHeight());
		(*it).toast->setRect(toast_rect);

		(*it).toast->setVisible(TRUE);	
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::showToastsTop()
{
}

//--------------------------------------------------------------------------
void LLScreenChannel::createOverflowToast(S32 bottom, F32 timer)
{
	LLRect toast_rect;
	LLToast::Params p;
	p.timer_period = timer;
	mOverflowToastPanel = new LLToast(p);

	if(!mOverflowToastPanel)
		return;

	mOverflowToastPanel->setOnFadeCallback(boost::bind(&LLScreenChannel::onOverflowToastHide, this));

	LLTextBox* text_box = mOverflowToastPanel->getChild<LLTextBox>("toast_text");
	LLIconCtrl* icon = mOverflowToastPanel->getChild<LLIconCtrl>("icon");
	std::string	text = llformat(mOverflowFormatString.c_str(),mHiddenToastsNum);
	if(mHiddenToastsNum == 1)
	{
		text += ".";
	}
	else
	{
		text += "s.";
	}

	toast_rect = mOverflowToastPanel->getRect();
	mOverflowToastPanel->reshape(getRect().getWidth(), toast_rect.getHeight(), true);
	toast_rect.setLeftTopAndSize(getRect().mLeft, bottom + toast_rect.getHeight()+gSavedSettings.getS32("ToastMargin"), getRect().getWidth(), toast_rect.getHeight());	
	mOverflowToastPanel->setRect(toast_rect);

	text_box->setValue(text);
	text_box->setVisible(TRUE);
	icon->setVisible(TRUE);

	mOverflowToastPanel->setVisible(TRUE);
}

//--------------------------------------------------------------------------
void LLScreenChannel::onOverflowToastHide()
{
	mOverflowToastHidden = true;
}

//--------------------------------------------------------------------------
void LLScreenChannel::closeOverflowToastPanel()
{
	if(mOverflowToastPanel != NULL)
	{
		mOverflowToastPanel->closeFloater();
		mOverflowToastPanel = NULL;
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::createStartUpToast(S32 notif_num, S32 bottom, F32 timer)
{
	LLRect toast_rect;
	LLToast::Params p;
	p.timer_period = timer;
	mStartUpToastPanel = new LLToast(p);

	if(!mStartUpToastPanel)
		return;

	mStartUpToastPanel->setOnFadeCallback(boost::bind(&LLScreenChannel::onStartUpToastHide, this));

	LLTextBox* text_box = mStartUpToastPanel->getChild<LLTextBox>("toast_text");
	LLIconCtrl* icon = mStartUpToastPanel->getChild<LLIconCtrl>("icon");

	std::string mStartUpFormatString;

	if(notif_num == 1)
	{
		mStartUpFormatString = LLTrans::getString("StartUpNotification");
	}
	else
	{
		mStartUpFormatString = LLTrans::getString("StartUpNotifications");
	}
	

	std::string	text = llformat(mStartUpFormatString.c_str(), notif_num);

	toast_rect = mStartUpToastPanel->getRect();
	mStartUpToastPanel->reshape(getRect().getWidth(), toast_rect.getHeight(), true);
	toast_rect.setLeftTopAndSize(getRect().mLeft, bottom + toast_rect.getHeight()+gSavedSettings.getS32("ToastMargin"), getRect().getWidth(), toast_rect.getHeight());	
	mStartUpToastPanel->setRect(toast_rect);

	text_box->setValue(text);
	text_box->setVisible(TRUE);
	icon->setVisible(TRUE);

	mStartUpToastPanel->setVisible(TRUE);
}

//--------------------------------------------------------------------------
void LLScreenChannel::updateStartUpString(S32 num)
{
	// *TODO: update string if notifications are arriving while the StartUp toast is on a screen
}

//--------------------------------------------------------------------------
void LLScreenChannel::onStartUpToastHide()
{
	onCommit();
}

//--------------------------------------------------------------------------
void LLScreenChannel::closeStartUpToast()
{
	if(mStartUpToastPanel != NULL)
	{
		mStartUpToastPanel->closeFloater();
		mStartUpToastPanel = NULL;
	}
}

//--------------------------------------------------------------------------
void LLScreenChannel::hideToastsFromScreen()
{
	closeOverflowToastPanel();
	for(std::vector<ToastElem>::iterator it = mToastList.begin(); it != mToastList.end(); it++)
		(*it).toast->setVisible(FALSE);
}

//--------------------------------------------------------------------------
void LLScreenChannel::removeToastsFromChannel()
{
	hideToastsFromScreen();
	for(std::vector<ToastElem>::iterator it = mToastList.begin(); it != mToastList.end(); it++)
	{
		deleteToast((*it).toast);
	}
	mToastList.clear();
}

//--------------------------------------------------------------------------
void LLScreenChannel::removeAndStoreAllStorableToasts()
{
	if(mToastList.size() == 0)
		return;

	hideToastsFromScreen();
	for(std::vector<ToastElem>::iterator it = mToastList.begin(); it != mToastList.end();)
	{
		if((*it).toast->getCanBeStored())
		{
			mStoredToastList.push_back(*it);
			mOnStoreToast((*it).toast->getPanel(), (*it).id);
			(*it).toast->stopTimer();
			it = mToastList.erase(it);
		}
		else
		{
			++it;
	}
	}
	redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::removeToastsBySessionID(LLUUID id)
{
	if(mToastList.size() == 0)
		return;

	hideToastsFromScreen();
	for(std::vector<ToastElem>::iterator it = mToastList.begin(); it != mToastList.end();)
	{
		if((*it).toast->getSessionID() == id)
		{
			deleteToast((*it).toast);
			it = mToastList.erase(it);
		}
		else
		{
			++it;
		}
	}
	redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::onToastHover(LLToast* toast, bool mouse_enter)
{
	// because of LLViewerWindow::updateUI() that ALWAYS calls onMouseEnter BEFORE onMouseLeave
	// we must check this to prevent incorrect setting for hovering in a channel
	std::map<LLToast*, bool>::iterator it_first, it_second;
	S32 stack_size = mToastEventStack.size();
	mIsHovering = mouse_enter;

	switch(stack_size)
	{
	case 0:
		mToastEventStack.insert(std::pair<LLToast*, bool>(toast, mouse_enter));
		break;
	case 1:
		it_first = mToastEventStack.begin();
		if((*it_first).second && !mouse_enter && ((*it_first).first != toast) )
		{
			mToastEventStack.clear();
			mIsHovering = true;
		}
		else
		{
			mToastEventStack.clear();
			mToastEventStack.insert(std::pair<LLToast*, bool>(toast, mouse_enter));
		}
		break;
	default:
		LL_ERRS ("LLScreenChannel::onToastHover: stack size error " ) << stack_size << llendl;
	}

	if(!mIsHovering)
		redrawToasts();
}

//--------------------------------------------------------------------------
void LLScreenChannel::updateShowToastsState()
{
	LLFloater* floater = LLDockableFloater::getInstanceHandle().get();

	if(!floater)
	{
		setShowToasts(true);
		return;
	}

	if(dynamic_cast<LLIMFloater*>(floater) || dynamic_cast<LLSysWellWindow*>(floater))
	{
		setShowToasts(!(floater->getVisible() && floater->isDocked()));
		if (!getShowToasts())
		{
			removeAndStoreAllStorableToasts();
		}
		
	}
}

//--------------------------------------------------------------------------